当前位置:Gxlcms >
数据库问题 >
mysql联查中使用if和group by会让你的结果不是你想要的
mysql联查中使用if和group by会让你的结果不是你想要的
时间:2021-07-01 10:21:17
帮助过:133人阅读
SQL_NO_CACHE t1.
*,t2.nick_name,t2.avatar,
IF(t1.
user_id=104080,
1,
0)
AS is_owner,
IF(t4.
user_id=104080,t4.vote_val,
‘N‘)
AS is_voted,
COUNT(
DISTINCT t5.
user_id)
AS voted_up_count,
COUNT(
DISTINCT tt.
user_id)
AS voted_down_count,
COUNT(
DISTINCT t6.id)
AS comment_count,
IF(t7.
user_id=104080,
1,
0)
AS is_collected
FROM answer t1
JOIN user t2
ON t1.
user_id = t2.id
LEFT JOIN answer_vote t4
ON t4.item_id
= t1.id
LEFT JOIN answer_vote t5
ON t5.item_id
= t1.id
AND t5.vote_val
= ‘U‘
LEFT JOIN answer_vote tt
ON tt.item_id
= t1.id
AND tt.vote_val
= ‘D‘
LEFT JOIN answer_comment t6
ON t6.item_id
= t1.id
LEFT JOIN answer_collection t7
ON t7.item_id
= t1.id
GROUP BY id
HAVING approved
= 1 AND question_id
= 100131
ORDER BY voted_up_count
desc,id
desc LIMIT
10 OFFSET
0;
未完待续......
mysql联查中使用if和group by会让你的结果不是你想要的
标签:color 第一条 sql count nbsp comment Owner vat 条件