php - MySQL Query for displaying all rows of one table with yes/no if matching other table? -
i apologize in advance non technincal description on problem!
i have 2 tables: usersoptions , optionslist..
for simplicity sake, optionslist below:
- id - name
 - 1 - red
 - 2 - blue
 - 3 - orange
 
usersoptions has many rows eg;
- id - client - option
 - 1 - john - red
 - 2 - john - orange
 - 3 - mary - red
 - 4 - jill - blue
 - 5 - jill - orange
 - etc..
 
is there query can run give me following output? (yes/no not essential)
 john's output:
- option - yes/no
 - red - y
 - blue - n
 - orange - y
 
 mary's output:
- option - yes/no
 - red - y
 - blue - n
 - orange - n
 
this driving me crazy! can help!
you can use case statement exists sub-query:
select   name,   case when exists         (select id usersoptions          client = 'john' , `option` = optionslist.name)        'y' else 'n' end `yes/no`   optionslist      
Comments
Post a Comment