MySQL Query无法正确显示

时间:2012-10-06 05:09:06

标签: mysql sql database

我不得不设置一个查询来检索客户上的最后评论,如果没有人对他们进行评论超过4周。我可以使用下面的查询使其工作,但由于某种原因,注释列将不会显示最新的记录。相反,它显示最旧的,但日期显示最新。这可能只是因为我在SQL上是一个菜鸟,但究竟我在这里做错了什么?

SELECT DISTINCT
    customerid, id, customername, user, MAX(date) AS 'maxdate', comment
FROM comments
WHERE customerid IN 
   (SELECT DISTINCT id FROM customers WHERE pastdue='1' AND hubarea='1') 
AND customerid NOT IN 
   (SELECT DISTINCT customerid FROM comments WHERE DATEDIFF(NOW(), date) <= 27)
GROUP BY customerid
ORDER BY maxdate

第一个“WHERE”子句只是确保它只显示来自特定区域的客户,并确保它们“已过期”。第二个确保客户在过去27天内未被评论过。它按customerid分组,因为这是与每个客户关联的数字。当我得到结果时,一切都是正确的,除了评论栏......任何想法?

5 个答案:

答案 0 :(得分:1)

更好地加入嵌套查询,因此您使用连接而不是嵌套查询 加入提高你的速度 此查询可以解决您的问题。

SELECT DISTINCT customerid,id, customername, user, MAX(date) AS 'maxdate', comment FROM comments inner join customers on comments.customerid = customers.id WHERE comments.pastdue='1' AND comments.hubarea='1' AND DATEDIFF(NOW(), comments.date) <= 27 GROUP BY customerid ORDER BY maxdate

答案 1 :(得分:0)

每个案例都需要有子查询。

SELECT  a.*
FROM    comments a
        INNER JOIN
        (
            SELECT customerID, max(`date`) maxDate
            FROM    comments
            GROUP BY customerID
        ) b ON a.customerID = b.customerID AND
                a.`date` = b.maxDate
        INNER JOIN
        (
            SELECT DISTINCT ID
            FROM customers
            WHERE pastdue = 1 AND hubarea = 1
        ) c ON c.ID = a.customerID
        LEFT JOIN
        (
            SELECT DISTINCT customerid 
            FROM comments 
            WHERE DATEDIFF(NOW(), date) <= 27
        ) d ON a.customerID = d.customerID
WHERE   d.customerID IS NULL
  • 第一次加入获取每位客户的最新记录。
  • 第二个联接仅显示来自特定区域的客户,并且它们“已过期”。
  • 使用LEFT JOIN的第三个联接选择过去27天内未评论过的所有客户。在这种情况下,由于条件d.customerID IS NULL,仅选择不在列表中的记录。

但是要缩短查询时间,如果customers表已经有unique个客户记录,那么就不需要在其上有子查询。直接加入表并将条件放在WHERE条款。

SELECT  a.*
FROM    comments a
        INNER JOIN
        (
            SELECT customerID, max(`date`) maxDate
            FROM    comments
            GROUP BY customerID
        ) b ON a.customerID = b.customerID AND
                a.`date` = b.maxDate
        INNER JOIN customers c
            ON c.ID = a.customerID
        LEFT JOIN
        (
            SELECT DISTINCT customerid 
            FROM comments 
            WHERE DATEDIFF(NOW(), date) <= 27
        ) d ON a.customerID = d.customerID
WHERE   d.customerID IS NULL AND
        c.pastdue = 1 AND 
        c.hubarea = 1

答案 2 :(得分:0)

试试这个

select  customerid, id, customername, user,date, comment from(
  select customerid, id, customername, user,date, comment, 
      @rank :=  IF(@current_customer = id, @rank+ 1, 1),
      @current_customer := id
    from comments
    where customerid IN 
      (SELECT DISTINCT id FROM customers WHERE pastdue='1' AND hubarea='1') 
      AND customerid NOT IN 
      (SELECT DISTINCT customerid FROM comments WHERE DATEDIFF(NOW(), date) <= 27)
    order by customerid, maxdate desc
) where rank <= 1

答案 3 :(得分:0)

我认为这可能会做你想要实现的目标。如果你可以执行它并且可能会报告它是否有,我可以根据需要进行调整。逻辑上,它“应该”起作用 - 如果我正确地理解了你的问题:)

SELECT X.customerid, X.maxdate,  co.id, c.customername, co.user, co.comment
FROM 
    (SELECT customerid, MAX(date) AS 'maxdate'
    FROM comments cm
    INNER JOIN customers cu ON cu.id = cm.customerid
    WHERE cu.pastdue='1' 
    AND cu.hubarea='1' 
    AND DATEDIFF(NOW(), cm.date) <= 27)
    GROUP BY customerid) X
INNER JOIN comments co ON X.customerid = co.customerid and X.maxdate = co.date
INNER JOIN customer c  ON X.customerid = c.id
ORDER BY X.maxdate

答案 4 :(得分:0)

您的两个表列不包含在聚合函数或GROUP BY子句中。例如,假设您有两个具有相同客户ID和相同日期的数据行,但具有不同的注释数据。 SQL应如何聚合这两行? :(它会产生错误......