mysql - Query for manytomany relation (also return if empty) -
i building popover, in can tick checkboxes. options , choices stored in manytomany relationship inside mysql database.
[ ] option [x] option b [ ] option c
there 3 tables. sphotos
, sphoto_feedback
, sphoto_has_feedbacks
. sphoto_has_feedbacks stores sphoto_id, sphoto_feedback_id , user_id, reference user has submitted voting.
sphoto id | status_id | ... 1 | ... sphoto_feedback id | name | ... 11 | quality | 12 | creative | sphoto_has_feedbacks id | sphoto_id | sphoto_feedback_id | user_id 1 | 1 | 11 | 9999
the input user_id => 9999
, sphoto_id => 1
. desired output array, has sphoto_feedback entrys, boolean variable, this:
$output = [ "0" => [ "id" => 11, "name" => "quality", "checked" => true ], "1" => [ "id" => 12, "name" => "creative", "checked" => false ] ]
it this:
[x] quality <-- stored in sphoto_feedback, stored in sphoto_has_feedbacks reference user [ ] creative <-- stored in sphoto_feedback
i want retrieve options database , check, if user has voted on options or not.
i know how in php 2 querys, i'd use 1 query , know if possible.
use left join
join sphoto_feedback
, sphoto_has_feedback
tables, returning null
rows in first table don't have match in second table.
select f.id, f.name, shf.id not null checked sphoto_feedback f left join sphoto_has_feedback shf on f.id = shf.sphoto_feedback_id , shf.sphoto_id = 1 , shf.user_id = 9999
Comments
Post a Comment