php - Join query shows results based only on one match -
so here query
select `c`.`location`, `c`.`id`, `c`.`user_id`, `c`.`date`, `c`.`attachment`, `ud`.`first_name`, `ud`.`last_name`, `a`.`file_name`, `a`.`folder_name`, `a`.`server_key`, `a`.`type` `content` `c` inner join `users_details` `ud` on ud.user_id = c.user_id inner join `attachments` `a` on c.id = a.content_id (c.location = 'new york')
i'm looking include in results data attachments table, problem not data row table content
has attachment, if post has attachments saves in content table in row attachment 1, , 0 if doesn't have attachment.
now problem query displays data has attachments, guess problem comes join attachments table.
so how can have following output: kinda merge data show posts have uploads , don't , show data attachments table have uploads. kinda this:
[0] => array(11) { ["location"] => string(15) "new york" ["id"] => string(2) "25" ["user_id"] => string(1) "1" ["date"] => string(10) "1364348772" ["attachment"] => string(1) "1" ["first_name"] => string(8) "john" ["last_name"] => string(7) "doe" ["file_name"] => string(36) "c2638acdac24dc2efe4e5971db5f4cc5.jpg" ["folder_name"] => string(13) "5133b99030ac4" ["server_key"] => string(1) "1" ["type"] => string(1) "1" } [1] => array(11) { ["location"] => string(15) "new york" ["id"] => string(2) "26" ["user_id"] => string(1) "1" ["date"] => string(10) "1364348812" ["attachment"] => string(1) "0" ["first_name"] => string(8) "john" ["last_name"] => string(7) "doe" ["file_name"] => string(36) "" ["folder_name"] => string(13) "" ["server_key"] => string(1) "" ["type"] => string(1) "" }
it seem need left outer join
attachments table this:
select `c`.`location`, `c`.`id`, `c`.`user_id`, `c`.`date`, `c`.`attachment`, `ud`.`first_name`, `ud`.`last_name`, `a`.`file_name`, `a`.`folder_name`, `a`.`server_key`, `a`.`type` `content` `c` inner join `users_details` `ud` on ud.user_id = c.user_id left outer join `attachments` `a` on c.id = a.content_id c.location = 'new york'
the outer join
return results regardless whether there a.content_id
matches c.id
. selected fields attachment table show null
.
Comments
Post a Comment