Schema: http://pastebin.com/jU3HmmNM
$mainfetch = "SELECT * FROM status ORDER BY status_id DESC LIMIT 10,0";
while($mainfetch -> $result) {
$subquery = SELECT COUNT(*) FROM status s, likes l WHERE s.status_id = l.status_id AND l.member_id = 1;
$liked = $query->row();
if($liked > 0) {
//liked
}else {
//not liked
}
}
So this is not optimal as $subquery will execute each time 10 times, I wish to add $subquery to $mainfetch, how can I join them two?
您只需要在s.status_id = l.status_id
上添加左连接,然后您还将得到l.status_id
:如果为null,则成员1不喜欢它,否则当然可以。
Using the schema you provided:
SELECT s.status_id, s.text_id, count(l.status_id) as like_count
FROM status AS s LEFT JOIN likes AS l ON (s.status_id = l.status_id AND l.member_id = 1)
group by s.status_id, s.text_id
ORDER BY status_id DESC
LIMIT 10,0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.