SQL-筛选的命名条件

时间:2019-12-06 10:53:35

标签: mysql sql

我编写了MySQL查询来按日期过滤和计数表中的项目,现在需要获得类似以下结果:

+-----+--------------------+
| cnt | creation_date_date |
+-----+--------------------+
| 90  | 2019-11-15         |
| 31  | 2019-11-16         |
| 9   | 2019-11-17         |
| 80  | 2019-11-18         |
| 40  | 2019-11-19         |
| 32  | 2019-11-20         |
| 132 | 2019-11-21         |
+-----+--------------------+

我得到了:

+-----+
| cnt |
+-----+
| 1   |
| 1   |
| 1   |
| 1   |
| 1   |
| 1   |
| 1   |
| 1   |
+-----+

在矿山条件下,我不知道在哪里声明名称creation_date_date

我的查询:

SELECT COUNT(*) as cnt
FROM wallet
WHERE created >= CAST('2019-11-15' AS DATE) 
AND created <= CAST('2019-12-06' AS DATE)
GROUP BY created

2 个答案:

答案 0 :(得分:0)

根据您的输出,我们只能猜测。您忘记显示带有计数的created列,因此我们无法确定计数是指什么。也许您的created列是日期时间,而您只想要日期?

SELECT date(created) as date_created, COUNT(*) AS cnt
FROM wallet
WHERE created >= date '2019-11-15' AND created < date '2019-12-07'
GROUP BY date(created)
ORDER BY date(created);

答案 1 :(得分:-1)

请在下面的查询中运行以获取所需的输出。

 SELECT COUNT(*) AS cnt
    ,created AS creation_Date_Date
FROM wallet
WHERE created>= CAST('2019-11-15' AS DATE)
    AND created<= CAST('2019-12-06' AS DATE)
GROUP BY created