1

我需要计算FirstExtracted特定日期的所有数据,并且需要计算LastExtracted同一日期的所有数据。所以,今天,我需要所有的FirstExtractedLastExtracted那个相等的2012-10-24

这是我到目前为止所拥有的,但它没有提出来LastExtracted。它输出与以下LastExtracted相同的计数FirstExtracted

(SELECT LastExtracted,FirstExtracted,
COUNT(FirstExtracted) AS FirstCount,
COUNT(LastExtracted) AS LastCount,
DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') AS Lastdate,
DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS Firstdate 
FROM results
WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d'))
UNION ALL
(SELECT LastExtracted,FirstExtracted,
COUNT(FirstExtracted) AS FirstCount,
COUNT(LastExtracted) AS LastCount,
DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') AS Lastdate,
DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS Firstdate 
FROM results
WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') GROUP BY Firstdate) 
ORDER BY Firstdate DESC
LIMIT 20

也许我应该使用内部连接?

更新:所以使用你的查询我做了一些改变,让它现在为我做其他事情。如果您查看此页面,我会提出查询

semiold.com/code2.html

我想计算所有标题和 GROUP BY 艺术家。这将是一个数组。然后我希望子查询按艺术家匹配的搜索类型计数。所以如果分贝有阿肯、蕾哈娜、克里斯布朗。我希望它计算每个艺术家有多少个标题,比如 akon 有 100 个。然后我想显示标题的数量,然后计算这 100 个标题中有多少是 google、bing、akon 特定站点等。

4

2 回答 2

0

我建议DISTINCT在您的查询中添加:

(SELECT LastExtracted,FirstExtracted,
COUNT(DISTINCT FirstExtracted) AS FirstCount,
COUNT(DISTINCT LastExtracted) AS LastCount,
DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') AS Lastdate,
DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS Firstdate 
FROM results
WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d'))
UNION ALL
(SELECT LastExtracted,FirstExtracted,
COUNT(DISTINCT FirstExtracted) AS FirstCount,
COUNT(DISTINCT LastExtracted) AS LastCount,
DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') AS Lastdate,
DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS Firstdate 
FROM results
WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') GROUP BY Firstdate) 
ORDER BY Firstdate DESC
LIMIT 20
于 2012-10-24T16:33:24.457 回答
0

我将使用联合分别获取两个结果(FirstExtracted 和 LastExtracted),然后使用子查询将它们合并。

SELECT
    sub.date, sum(sub.FirstCount) AS FirstCount,
    sum(sub.LastCount) AS LastCount 
  FROM (
      SELECT
          DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS date,
          COUNT(FirstExtracted) AS FirstCount, 0 AS LastCount
        FROM results
        GROUP BY date
      UNION ALL
        SELECT
            DATE_FORMAT(`LastExtracted`,'%Y-%m-%d'),
            0, COUNT(LastExtracted)
          FROM results
          GROUP BY date
    ) AS sub 
    GROUP BY sub.date

假设您今天首次提取了 220 个条目,而今天最近提取了 292 个条目。在其他日期的条目中,这将为您提供:

+------------+------------+-----------+
| date       | FirstCount | LastCount |
+------------+------------+-----------+
| 2012-10-24 |        220 |       292 |
+------------+------------+-----------+

更新UNION单独会给你以下结果。注意零占位符。

+------------+------------+-----------+
| date       | FirstCount | LastCount |
+------------+------------+-----------+
| 2012-10-24 |        220 |         0 |
+------------+------------+-----------+
| 2012-10-24 |          0 |       292 |
+------------+------------+-----------+
于 2012-10-24T17:17:47.970 回答