Mysql 选择昨天的日期

Mysql 选择昨天的日期,mysql,Mysql,如何显示和计算日期为昨天的值? 我用时间在数据库中插入日期。例如: URL: google.com youtube.com google.com youtube.com test.com youtube.com DateVisited: 1313668492 1313668540 1313668571 13154314 我想显示表中存在多个URL的数量,以及昨天访问过的URL数量。示例结果: LINK | timesExisted | timesVisitedYesterday G

如何显示和计算日期为昨天的值? 我用时间在数据库中插入日期。例如:

URL: google.com youtube.com google.com youtube.com test.com youtube.com
DateVisited: 1313668492 1313668540 1313668571 13154314
我想显示表中存在多个URL的数量,以及昨天访问过的URL数量。示例结果:

LINK       | timesExisted | timesVisitedYesterday
Google.com |       2      | 2
youtube.com|       3      | 3

我已经有了获取昨天日期的想法,但我不知道如何计算昨天URL存在的次数以及表中URL存在的次数。

您可以使用表达式CASTNOW-INTERVAL 1 DAY作为日期来获取昨天的日期。所以类似的方法可能会奏效:

SELECT * FROM your_table

WHERE DateVisited >= UNIX_TIMESTAMP(CAST(NOW() - INTERVAL 1 DAY AS DATE))
  AND DateVisited <= UNIX_TIMESTAMP(CAST(NOW() AS DATE));

获取昨天日期的最简单、最好的方法是:

subdate(current_date, 1)
你的问题是:

SELECT 
    url as LINK,
    count(*) as timesExisted,
    sum(DateVisited between UNIX_TIMESTAMP(subdate(current_date, 1)) and
        UNIX_TIMESTAMP(current_date)) as timesVisitedYesterday
FROM mytable
GROUP BY 1
出于好奇,sumcondition为您提供满足条件的行数的原因是,在mysql中,布尔值为1表示true,0表示false,因此求和一个条件有效地计算出它为true的次数,否则需要一个繁琐而冗长的case语句。使用此模式可以整理SQL代码

SELECT SUBDATE(NOW(),1);
其中现在函数在时间戳中返回系统的当前日期和时间

您可以使用:


我从cdhowie中改编了上面的一个答案,因为我无法让它工作。这似乎对我有用。我怀疑在使用UNIX_TIMESTAMP函数的情况下也可以这样做

SELECT * FROM your_table

WHERE UNIX_TIMESTAMP(DateVisited) >= UNIX_TIMESTAMP(CAST(NOW() - INTERVAL 1 DAY AS DATE))
  AND UNIX_TIMESTAMP(DateVisited) <= UNIX_TIMESTAMP(CAST(NOW() AS DATE));

最近几周的查询:

SELECT *
FROM dual
WHERE search_date BETWEEN SUBDATE(CURDATE(), 7) AND CURDATE()

虽然选择的答案正确且更简洁,但我认为其他答案中的结构是正确的:

SELECT * FROM your_table
WHERE UNIX_TIMESTAMP(DateVisited) >= UNIX_TIMESTAMP(CAST(NOW() - INTERVAL 1 DAY AS DATE))
  AND UNIX_TIMESTAMP(DateVisited) <= UNIX_TIMESTAMP(CAST(NOW() AS DATE));
如果您只需要一个没有时间戳的裸日期,也可以按以下方式编写:

SELECT * FROM your_table
WHERE DateVisited >= CAST(NOW() - INTERVAL 1 DAY AS DATE)
  AND DateVisited <= CAST(NOW() AS DATE);
使用强制转换与子日期强制转换的原因是ANSI SQL语法。SUBDATE是CAST的日期算术组件的特定于MySQL的实现。如果需要迁移到其他数据库,养成使用ANSI语法的习惯可以减少麻烦。作为一种专业实践,养成这种习惯也是很好的,因为将来几乎肯定会与其他DBMS一起工作

没有一个主要的DBMS系统是完全符合ANSI标准的,但是大多数DBMS系统都实现了广泛的ANSI语法,而MySQL及其子系统MariaDB、Percona等之外的DBMS系统几乎都不会实现MySQL特定的语法。

您可以使用:


上一个或下一个日期、周、月和年的计算。这可能对任何人都有帮助

当前日期:

select curdate();
昨天:

select subdate(curdate(), 1)
明天:

select adddate(curdate(), 1)
上周:

select between subdate(curdate(), 7) and subdate(curdate(), 1)
between adddate(curdate(), 7) and adddate(curdate(), 1)
下一周:

select between subdate(curdate(), 7) and subdate(curdate(), 1)
between adddate(curdate(), 7) and adddate(curdate(), 1)
最近1个月:

between subdate(curdate(), 30) and subdate(curdate(), 1)
下一个月:

between adddate(curdate(), 30) and adddate(curdate(), 1)
当月:

subdate(curdate(),day(curdate())-1) and last_day(curdate());
过去1年:

between subdate(curdate(), 365) and subdate(curdate(), 1)
未来1年:

between adddate(curdate(), 365) and adddate(curdate(), 1)

为我工作。很多关于为什么一个人会选择一个而不是另一个的解释都很好。与其他投票率更高的答案相比,我更喜欢任何使用自我记录语言的解决方案,比如1天,而不是一个神奇的数字1作为论点。如果在性能上没有太大的影响,人性化的代码将优于Ank you@MartinJoiner
subdate(curdate(),day(curdate())-1) and last_day(curdate());
between subdate(curdate(), 365) and subdate(curdate(), 1)
between adddate(curdate(), 365) and adddate(curdate(), 1)