mysql - 如何比较where子句中的时间戳

标签 mysql sql timestamp where-clause

我有一个包含时间戳列的表,如果月份是 31 天,我想获取特定月份时间戳的值(例如 9 月 1 日到 9 月 30 日之间的时间戳)。 我使用这个查询:

SELECT users.username, users.id, count(tahminler.tahmin)as tahmins_no FROM users LEFT JOIN tahminler ON users.id = tahminler.user_id  GROUP BY users.id having count(tahminler.tahmin) > 0

我可以添加 where timestamp IN(dates_array) 吗??

date_array 将是整个月的日期??

最佳答案

SELECT users.username, users.id, count(tahminler.tahmin)as tahmins_no 
FROM users 
LEFT JOIN tahminler ON users.id = tahminler.user_id  
where year(timestamp) = 2013 and month(timestamp) = 9
GROUP BY users.id 
having count(tahminler.tahmin) > 0

To make it work with indexes you can do

SELECT users.username, users.id, count(tahminler.tahmin)as tahmins_no 
FROM users 
LEFT JOIN tahminler ON users.id = tahminler.user_id  
where timestamp >= '2013-09-01' and timestamp < '2013-10-01'
GROUP BY users.id 
having count(tahminler.tahmin) > 0

关于mysql - 如何比较where子句中的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18917871/

相关文章:

mysql - 如何使用存储过程获取两个表的总数?

php - 我如何找到 php session 开始时间戳

java - 仅用于审计目的的 ER 建模字符串对象列表

带案例的 Mysql 数据透视表

sql - SSIS 截断表失败;删除成功

java - 使用 hibernate 获取具有实体属性值的物理列值

java - 仍然对 Java 时间戳等与 MySQL 混淆

python - 组合/合并排序的 pandas 数据框中的时间间隔行

mysql - 在 MySQL 中使用 group by 时 count(*) 不返回 0

php - 出现错误 SQLSTATE[HY000] : General error: 2031 when trying to execute if statement