mysql - SQL - Combine a select * query with select 1 -


i'm trying combine data 2 tables in 1 result. simplified example-tables, want in result:

id    text                choice -------------------------------- 1     first choice     0 2     second choice    0 3     third choice     1 

from 2 tables persons , choosen

'persons':

id      name    age ------------------- 1       adam    22 2       scott   25 3       tom     28 

'choices':

id    text ---------------------- 1     first choice 2     second choice 3     third choice 

'choices_made':

person_id    choice_id ---------------------- 2            3 

i have tried different queries, not found right one. got stuck when trying query:

select * ( (select * choices) t1 union (select 1 choice choices_made person_id=2) t2 ) t_union 

... not work. causes error #1064 - have error in sql syntax.

any suggestions on how can accomplish wanted result?

you can try next query

select choices.*       ,if(choices_made.choice_id null, 0, 1) choice choices      left join choices_made on choices_made.choice_id = choices.id , choices_made.person_id = @personid 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -