问题描述
我正在尝试构建一个产品评论页面,用户可以在其中评论任何提交的评论,例如亚马逊.我希望页面显示 3 条用户评论以及对每条评论的 2 回复(如果存在).
I am trying to build a product review page where users can comment any submitted reviews like Amazon. I want the page to display 3 user reviews as well as 2 responses to each of the reviews if they exist.
这是表格
CREATE TABLE product_review
(`ID` int, `username` varchar(21), `review_title` varchar(30), `review_or_reply` int)
;
INSERT INTO product_review
(`ID`, `username`, `review_title`, `review_or_reply`)
VALUES
(1, 'Tom', 'Rip-off', 0),
(2, 'Peter', 'Rip-off', 1),
(3, 'May', 'Rip-off', 1),
(4, 'June', 'Rip-off', 1),
(5, 'Tommy', 'Worth the Price', 0),
(6, 'Sammy', 'Worth the Price', 1),
(7, 'Sam', 'Worth the Price',1),
(8, 'Bryan','Worth the Price',1),
(9, 'Sally', 'Average Product', 0)
;
review_or_reply 字段实际上是一个 Yes 或 No 字段,其中 0
表示这是一条评论,1
是其他用户对该评论的评论.
The review_or_reply field is effectively a Yes or No field, where 0
means it's a review and 1
is the review's comments by other users.
是否有一个 select 语句可以限制 3 条评论并提出两条评论?例如:
Is there a single select statement that can limit 3 reviews and bring up two of their comments? For example:
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_titles,然后拉出两个回复
this will return 3 review_titles and then pull out two responses to that
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).
if there are more than one reply that is 0 in the database like so then this query will check for that. (since you did specify in the other queries that the reply had to be 1).
INSERT INTO product_review
(`ID`, `username`, `review_title`, `reply`)
VALUES
(1, 'Tom', 'Rip-off', 0),
(2, 'Peter', 'Rip-off', 1),
(3, 'May', 'Rip-off', 0),
(4, 'June', 'Rip-off', 1),
(5, 'Tommy', 'Worth the Price', 0),
(6, 'Sammy', 'Worth the Price', 1),
(7, 'Sam', 'Worth the Price',1),
(8, 'Bryan','Worth the Price',1),
(9, 'Sally', 'Average Product', 0),
(10, 'Timothy', 'Rip-off', 1)
注意,在 id 3 处有 0 的回复,而 id 10 的回复为 1.此查询将正确跳过回复 = 0.
notice that at id 3 there is a reply of 0 with id 10 a reply of 1. this query will correctly skip the reply = 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;
这篇关于在一个语句中包含两个限制子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!