PDA

View Full Version : Mysql Join?


SWI03
05-07-2005, 05:10 AM
I have 3 tables

1st table has
county_id
county_name
state_id

2nd table has
state_id
state_name
state_abbr

3rd table has
id
first_name
last_name
county_id
state_id
email
and some other fields that are filled out in a form.

For the form, I get the state_name and county_name and put them in a dropdown box for each of them, but I would like to have it insert the id's for county and state in the 3rd table, or is it just not worth it to do that and just insert the state and county name? I've tried doing it but cannot seem to figure out how to convert the name to id and insert it in the database for the 3rd table. I am using php to put the info into the database.

NancyJ
05-07-2005, 03:09 PM
in the drop down boxes set the values to the ID rather than the name, ie

<select>
<option value = "$ID">"$name"</option>
</select>

This thread might be better in one of the scripting forums (depending on language)

Tangerine Dream
05-07-2005, 04:27 PM
For the form, I get the state_name and county_name and put them in a dropdown box for each of them, but I would like to have it insert the id's for county and state in the 3rd table, or is it just not worth it to do that and just insert the state and county name?
Hi, it's a good tone and useful to post table names too. Suppose you want to get 'country_name' ('country' table) and 'state_name' ('state' table) for user ('user' table) with given id (@user_id variable):



$sql="SELECT c.country_name AS country_name, s.state_name AS state_name
FROM сountry AS c, state AS s, user AS u
WHERE c.county_id = u.country_id AND s.state_id = u.state_id AND u.id = $user_id";



This thread might be better in one of the scripting forums (depending on language)
The question mostly about use of JOIN :)