Click here to Skip to main content
16,020,080 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 tables:

Table 1: Attributes
contents are 2 fields
attrID ; attribute
DV ; Diver
CL ; Climber
FF ; Fire Fighter

Table 2: userAttributes
contents are 2 fields
attrFK ; username
CL ; Peter
FF ; Peter
CL ; Joe

Then I have to dropdown lists in html and php, I need a select query so that when I click on one dropdown which holds the usenames and select it, the other dropdown list will show only those missing attributes of the users

Can any one help me?

Thanks
Posted

1 solution

Try this:
SQL
SELECT t1.username, t2.attribute
FROM userAttributes AS t1 INNER JOIN Attributes AS t2 ON t1.attrFK = t2.attrID
WHERE t1.username = @username


[EDIT #1 - missing attributes]
SQL
SELECT attribute AS MissingAttribute
FROM Attributes 
WHERE attrID NOT IN (
    SELECT t2.attrFK AS AttrID
    FROM userAttributes AS t1 INNER JOIN Attributes AS t2 ON t1.attrFK = t2.attrID
    WHERE t1.username = @username)


[EDIT]

More: IN (T-SQL)[^]
 
Share this answer
 
v3
Comments
datt265 9-May-13 14:45pm    
Thanks for your reply, with this I can get the attributes that the user already has, I want the inverse, that is the attributes missing to the user so that when I select the dropdown list I can add them to the user attributes
Maciej Los 9-May-13 14:48pm    
OK, sorry, my mistake. I'll improve my answer ASAP.
datt265 9-May-13 15:09pm    
I am getting
Unknown column 't2.attrFK' in 'field list'
Maciej Los 9-May-13 15:11pm    
Replace t2.attrFK with t1.attrFK.
datt265 9-May-13 15:21pm    
ok now thanks alot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900