Mysql 在一份声明中包含两个限制条款?
我正试图建立一个产品评论页面,用户可以评论任何提交的评论,比如亚马逊。我希望页面显示3用户评论以及对每个评论的2响应(如果存在) 这是我的建议 “审阅”或“答复”字段实际上是一个“是”或“否”字段,其中Mysql 在一份声明中包含两个限制条款?,mysql,sql,Mysql,Sql,我正试图建立一个产品评论页面,用户可以评论任何提交的评论,比如亚马逊。我希望页面显示3用户评论以及对每个评论的2响应(如果存在) 这是我的建议 “审阅”或“答复”字段实际上是一个“是”或“否”字段,其中0表示审阅,1表示其他用户对审阅的评论 是否有一个select语句可以限制3条评论并提出其中两条评论?例如: Select `username`,`review_title`,`reply` from product_review where review_or_reply ='0' Limit
0
表示审阅,1
表示其他用户对审阅的评论
是否有一个select语句可以限制3条评论并提出其中两条评论?例如:
Select `username`,`review_title`,`reply` from product_review where review_or_reply ='0' Limit 3
Select `username`,`review_title`,`reply` from product_review where review_or_reply = '1' and title = 'Rip-off' Limit 2
Select `username`,`review_title`,`reply` from product_review where review_or_reply = '1' and title = 'Worth the Price' Limit 2
Select `username`,`review_title`,`reply` from product_review where review_or_reply = '1' and title = 'Average Product' Limit 2
我希望输出如下:
username review_title review_or_reply
Tom Rip-off 0
Peter Rip-off 1
May Rip-off 1
Tommy Worth the Price 0
Sammy Worth the Price 1
Sam Worth the Price 1
Sally Average Product 0
这将返回3个review_标题,然后拉出两个回复
SELECT
pr.*,
IF( @A = t.review_title,
IF(@B = 3, @B := 1, @B := @B +1)
, @B
) AS group_col,
@A := t.review_title
FROM (
SELECT
id,
username,
review_title
FROM product_review
WHERE reply ='0' LIMIT 3
) t
JOIN product_review pr ON pr.review_title=t.review_title
CROSS JOIN (SELECT @A := "", @B := 1) AS temp
GROUP BY group_col, review_title
ORDER BY id;
编辑: 如果数据库中有一个以上的回复是0,那么此查询将检查该回复。(因为您在其他查询中指定了答复必须为1) 注意在id为3时,有一个0的回复,id为10,回复为1。此查询将正确跳过答复=0
SELECT
pr.*,
IF( @A = t.review_title,
IF(pr.reply = 0, 1,
IF(@B = 3, @B := 1, @B := @B +1)
), @B
) AS group_col,
@A := t.review_title
FROM (
SELECT
DISTINCT
id,
username,
review_title
FROM product_review
WHERE reply ='0'
GROUP BY review_title
LIMIT 3
) t
JOIN product_review pr ON pr.review_title=t.review_title
CROSS JOIN (SELECT @A := "", @B := 1) AS temp
GROUP BY group_col, review_title
ORDER BY id;
…或更慢但更简单
SELECT x.*
FROM product_review x
JOIN product_review y
ON y.review_title = x.review_title
AND y.id <= x.id
GROUP
BY x.id
HAVING COUNT(*) <= 3
ORDER
BY MIN(y.id)
LIMIT 3;
选择x.*
来自product_review x
加入产品回顾
在y.review\u title=x.review\u title上
y.id使用UNION ALL
收集你想要的结果是什么样子?@JohnRuddell我已经更新了帖子,请再次查看。你正在查找前3篇评论的前2条评论?@Andreas,是的,每个评论都有自己的评论。我希望每个评论都显示2条自己的评论exist@RedGiant我刚刚更新了查询。请使用更新的查询。当回复为0时,它不会出错。。。基本上,如果有两个0的回复,那么这也会解决这个问题。
SELECT
pr.*,
IF( @A = t.review_title,
IF(pr.reply = 0, 1,
IF(@B = 3, @B := 1, @B := @B +1)
), @B
) AS group_col,
@A := t.review_title
FROM (
SELECT
DISTINCT
id,
username,
review_title
FROM product_review
WHERE reply ='0'
GROUP BY review_title
LIMIT 3
) t
JOIN product_review pr ON pr.review_title=t.review_title
CROSS JOIN (SELECT @A := "", @B := 1) AS temp
GROUP BY group_col, review_title
ORDER BY id;
SELECT x.*
FROM product_review x
JOIN product_review y
ON y.review_title = x.review_title
AND y.id <= x.id
GROUP
BY x.id
HAVING COUNT(*) <= 3
ORDER
BY MIN(y.id)
LIMIT 3;