简体   繁体   English

如何选择最后一天插入的所有行?

[英]How can I select all rows which have been inserted in the last day?

I have a table like this: 我有这样一张桌子:

// reset_password_emails
+----+----------+--------------------+-------------+
| id | id_user  |        token       | unix_time   |
+----+----------+--------------------+-------------+
| 1  | 2353     | 0c274nhdc62b9dc... | 1339412843  |
| 2  | 2353     | 0934jkf34098joi... | 1339412864  |
| 3  | 5462     | 3408ujf34o9gfvr... | 1339412894  |
| 4  | 3422     | 2309jrgv0435gff... | 1339412899  |
| 5  | 3422     | 34oihfc3lpot4gv... | 1339412906  |
| 6  | 2353     | 3498hfjp34gv4r3... | 1339412906  |
| 16 | 2353     | asdf3rf3409kv39... | 1466272801  |
| 7  | 7785     | 123dcoj34f43kie... | 1339412951  |
| 9  | 5462     | 3fcewloui493e4r... | 1339413621  |
| 13 | 8007     | 56gvb45cf3454g3... | 1339424860  |
| 14 | 7785     | vg4er5y2f4f45v4... | 1339424822  |
+----+----------+--------------------+-------------+

Each row is an email. 每行都是一封电子邮件。 Now I'm trying to implement a limitation for sending-reset-password email. 现在我正在尝试实施发送重置密码电子邮件的限制。 I mean an user can achieve 3 emails per day (not more) . 我的意思是用户每天可以发送3封电子邮件(不是更多)

So I need an query to check user's history for the number of emails: 所以我需要一个查询来检查用户的电子邮件数量的历史记录:

SELECT count(1) FROM reset_password_emails WHERE token = :token AND {from not until last day}

How can I implement this: 我该如何实现这个:

. . . {from now until last day}

Actually I can do that like: NOW() <= (unix_time + 86400) .. But I guess there is a better approach by using interval . 实际上我可以这样做: NOW() <= (unix_time + 86400) ..但我想通过使用interval有一个更好的方法。 Can anybody tell me what's that? 谁能告诉我那是什么意思?

Your expression will work, but has 3 problems: 你的表达方式有效,但有3个问题:

  1. the way you've coded it means the subtraction must be performed for every row (performance hit) 你编码它的方式意味着必须对每一行执行减法(性能命中)
  2. because you're not using the raw column value, you couldn't use an index on the time column (if one existed) 因为您没有使用原始列值,所以您无法在时间列上使用索引(如果存在)
  3. it isn't clear to read 目前尚不清楚

Try this: 尝试这个:

unix_time > unix_timestamp(subdate(now(), interval '1' day))

here the threshold datetime is calculated once per query, so all of the problems above have been addressed. 这里每个查询计算一次阈值日期时间,因此上述所有问题都已得到解决。

See SQLFiddle demo 请参阅SQLFiddle演示

You can convert your unix_time using from_unixtime function 您可以将您unix_time使用from_unixtime功能

select r.*
  from reset_password_emails r
 where now() <= from_unixtime(r.unix_time) - interval '1' day

Just add the extra filters you want. 只需添加您想要的额外过滤器。

See it here: http://sqlfiddle.com/#!9/4a7a9/3 请在此处查看: http//sqlfiddle.com/#!9/4a7a9/3

It evaluates to no rows because your given data for unix_time field is all from 2011 它评估为无行,因为unix_time字段的给定数据全部来自2011

Edited with a sqlfiddle that show the conversion: 使用显示转换的sqlfiddle编辑:

http://sqlfiddle.com/#!9/4a7a9/4 http://sqlfiddle.com/#!9/4a7a9/4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM