gpt4 book ai didi

php - 如何为make循环编写左连接

转载 作者:行者123 更新时间:2023-11-29 10:45:03 25 4
gpt4 key购买 nike

我想用 left join 语句在一个查询中编写此代码。目前我使用 php for 循环这个查询,但它不好而且太慢

该应用程序具有喜欢不喜欢选项。我想要每个用户的数量,比如

     public function getUserLikedCount($id) {
$query = $this->db->query(
"SELECT * FROM `users`, post, `like`
WHERE users.id = post.post_user_id
AND like.like_post_id = post.post_id
AND users.id = ?
AND post.post_is_active = 1", array($id)
);
return $query->num_rows();
}

谢谢

最佳答案

尝试一下(我不知道你的表like结构):

$sql = 'SELECT u.id AS `user_id`
COUNT(p.post_id) AS `liked_cnt` <-- or like.id ( if this field exists)
FROM `post` AS p
LEFT JOIN `users` AS u
ON ( u.id = p.post_user_id )
LEFT JOIN `like` AS l
ON ( l.like_post_id = p.post_id )
WHERE p.post_is_active = 1
AND l.status = 1 <-- "liked"
GROUP BY u.id ';

关于php - 如何为make循环编写左连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44744453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com