MySQL DISTINCT没有过滤掉

时间:2011-11-08 11:52:45

标签: mysql sql

我有以下的SQL查询:

SELECT DISTINCT(tbl_products.product_id), tbl_products.product_title,
            tbl_brands.brand_name, tbl_reviews.review_date_added, 
            NOW() AS time_now
            FROM tbl_products, tbl_reviews, tbl_brands
            WHERE tbl_products.product_id = tbl_reviews.product_id AND
            tbl_products.brand_id = tbl_brands.brand_id
            ORDER BY tbl_reviews.review_date_added DESC

需要过滤掉任何重复的product_id,不幸的是选择tbl_reviews.review_date_added会使每条记录都唯一,这意味着DISTINCT将不再有效。

还有其他方法可以执行此查询,以便product_id仍然是唯一的吗?

我确实做过GROUP BY,问题是我在网站上显示tbl_reviews.review_date_added并选择最早的日期。我需要最新的日期。

此致

4 个答案:

答案 0 :(得分:2)

根据给出的描述,有点难以确定,但如果review_date_added是唯一的问题,那么似乎就像你想要那个日期的MAX()一样?

如果以下内容没有帮助,请您提供示例数据,示例输出以及您希望如何创建输出的说明?

SELECT
  tbl_products.product_id,
  tbl_products.product_title,
  tbl_brands.brand_name,
  MAX(tbl_reviews.review_date_added) AS review_date_added,
  NOW() AS time_now
FROM
  tbl_products
INNER JOIN
  tbl_reviews
    ON tbl_products.product_id = tbl_reviews.product_id
INNER JOIN
  tbl_brands
    ON tbl_products.brand_id = tbl_brands.brand_id
GROUP BY
  tbl_products.product_id,
  tbl_products.product_title,
  tbl_brands.brand_name
ORDER BY
  MAX(tbl_reviews.review_date_added) DESC

答案 1 :(得分:1)

试试这个:

SELECT pr.product_id, pr.product_title,
       bd.brand_name, 
      (SELECT MAX(rev.review_date_added) FROM tbl_reviews rev
       WHERE pr.product_id = rev.product_id) AS maxdate, 
       NOW() AS time_now
FROM tbl_products pr INNER JOIN tbl_reviews re 
    ON pr.product_id = re.product_id
INNER JOIN tbl_brands bd
    ON pr.brand_id = bd.brand_id
GROUP BY pr.product_id
ORDER BY re.review_date_added DESC

或(由@Hogan建议)

SELECT pr.product_id, pr.product_title,
       bd.brand_name, md.maxdate
       NOW() AS time_now
FROM tbl_products pr INNER JOIN tbl_reviews re 
    ON pr.product_id = re.product_id
INNER JOIN tbl_brands bd
    ON pr.brand_id = bd.brand_id
INNER JOIN (SELECT product_id, MAX(review_date_added) AS maxdate 
            FROM tbl_reviews rev GROUP BY product_id) md
    ON pr.product_id = md.product_id
GROUP BY pr.product_id
ORDER BY re.review_date_added DESC

答案 2 :(得分:1)

区别适用于整行。括号就在这个领域:

distinct (a), b, c  ===  distinct a, b, c

一个简单的解决方案是group by。您可以使用min选择最早的日期。

select  tbl_products.product_id
,       min(tbl_products.product_title)
,       min(tbl_brands.brand_name)
,       min(tbl_reviews.review_date_added)
,       NOW() AS time_now
FROM    tbl_products, tbl_reviews, tbl_brands
WHERE   tbl_products.product_id = tbl_reviews.product_id AND
        tbl_products.brand_id = tbl_brands.brand_id
GROUP BY
        tbl_products.product_id
ORDER BY 
        min(tbl_reviews.review_date_added) DESC

请注意,如果某个产品可以包含多个品牌,则会选择最低的品牌。

答案 3 :(得分:0)

我将Andomar的答案与你在这里找到的一些变化结合起来。

SELECT tbl_products.product_id, tbl_products.product_title,
                    tbl_products.product_date_added, tbl_brands.brand_name,
                    MAX(tbl_reviews.review_date_added) AS review_date_added, NOW() AS time_now
            FROM tbl_products, tbl_reviews, tbl_brands
            WHERE tbl_products.product_id = tbl_reviews.product_id AND
                    tbl_products.brand_id = tbl_brands.brand_id
            GROUP BY tbl_products.product_id
            ORDER BY MAX(tbl_reviews.review_date_added) DESC

精美地工作,并在tbl_reviews.review_date_added显示最新日期。

此致