PDA

View Full Version : MySQL Query Help!!!?


Pete DaVilbus
10-11-2007, 10:19 AM
I am using the following query in PHP to print a list of horse attributes that have been assigned to a selected breed.

$query1 = "SELECT * FROM attribute LEFT JOIN breed_to_attribute ON breed_to_attribute.att_id = attribute.id WHERE breed_to_attribute.breed_id = $id";

I am also using this query to populate a select box where new attributes may be assigned.

$query2 = "SELECT * FROM attribute";

I'd like to make the available attributes in the selection box (query2) include only those attributes that are not in use already (query1).

For instance, if all the possible attributes are

height
weight
age
sex

and height and age are already assigned to the breed, the dropdown list would include only weight and sex.

I just can't seem to figure it out. Can someone help?

dickersonka
10-11-2007, 10:19 AM
the best is way to get rid of the *
keep track php side what you are selecting in query1
then change your query2 to reflect that
just in case you add columns later, you don't want the * to affect your results

quantumkev
10-11-2007, 10:20 AM
I would use a nested query, something like this :

SELECT * FROM attribute WHERE attribute.id
NOT IN (SELECT attribute.id FROM attribute LEFT JOIN breed_to_attribute_att.id.....)

I think that should work for you. Basically, you just select information based on the attribute id's that are NOT IN your first query. You may have to tweak this a little bit, but hopefully it will give you the basic idea. Good luck : )