两个限制条款在一个声明中?

时间:2014-07-24 14:36:22

标签: mysql sql

我正在尝试构建一个产品评论页面,用户可以在其中评论任何提交的评论,例如亚马逊。我希望该页面显示 3 用户评论以及 2 对每条评论的回复(如果存在)。

这是table

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字段实际上是“是”或“否”字段,其中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

2 个答案:

答案 0 :(得分:1)

这将返回3个review_titles,然后提取两个回复

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)。

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.

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;

DEMO

答案 1 :(得分:0)

......或者更慢但更简单...

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;