1

如果没有人对他们发表评论超过 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 分组,因为这是与每个客户相关联的数字。当我得到结果时,一切都是正确的,除了评论栏......有什么想法吗?

4

5 回答 5

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

于 2012-10-06T05:47:10.417 回答
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
于 2012-10-06T05:32:22.257 回答
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
于 2012-10-06T05:35:56.860 回答
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
于 2012-10-06T05:40:56.497 回答
0

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

于 2012-10-06T05:45:11.270 回答