在SQL中的日期列中迭代

时间:2018-02-02 00:23:31

标签: mysql sql

MySQL新秀在这里。

我有这张桌子:

+-------------+-----------------------+
| business_id |     date_creation     |
+-------------+-----------------------+
| '3610'      | '2012-08-19 07:43:05' |
| '5020'      | '2012-08-18 03:38:51' |
| '5020'      | '2012-08-18 03:38:51' |
| '9491'      | '2017-08-18 06:52:52' |
| '10389'     | '2012-08-18 07:08:50' |
| ...         |                       |
+-------------+-----------------------+

我通过这个查询得到了这个:

SELECT
  citation.business_id,
  citation.date_creation
FROM citation,
     business
WHERE primary_activity_id = 850
AND business.id = citation.business_id;

但是,我想只过滤2017年的日期。

2 个答案:

答案 0 :(得分:1)

您可以像这样更新您的查询:

SELECT
  citation.business_id,
  citation.date_created
FROM citation,
     business
WHERE primary_activity_id = 850
AND business.id = citation.business_id
AND YEAR(citation.date_created) >= 2017;

答案 1 :(得分:1)

您可以添加AND子句以将日期列值与日期字面值进行比较。

SELECT
  citation.business_id,
  citation.date_created
FROM citation,
     business
WHERE primary_activity_id = 850
AND business.id = citation.business_id
AND citation.date_created >= '2017-01-01 00:00:00';