我有一个表,其中包含带有日期列的数据。需要Mysql查询帮助!
现在,我在我的列中使用 php 时间戳 (time()) 作为时间戳。我使用它是因为我认为将其存储为整数比使用日期时间更有效。
现在我在选择两个时间戳之间的时间时遇到严重问题。
例如,我试图获取昨天添加的记录(即在 php 时间戳中的今天日期和 php 时间戳中的 Todaysdate - 24 小时之间)
,它在 sql 伪代码中看起来像这样,
SELECT * FROM table
WHERE date < phptimestamp(todaysdate) AND date > phptimestamp(todaysdate -24h)
它确实为我提供了一些记录,但由于某种原因,之间的记录这个范围不断变化。我正在查找的时间戳是静态的。这些时间戳之间的记录不应更改,因为添加的所有记录都具有当前时间戳。
真实代码
$date = getdate();
$m = time() - (5 * 60);
$h = time() - (($date['minutes'] * 60) + $date['seconds']);
$d = time() - ((60 * 60 * $date['hours']) + ($date['minutes'] * 60) + $date['seconds']);
$y = time() - ((60 * 60 * 24) + ($date['hours'] * 60 * 60) + ($date['minutes'] * 60) + $date['seconds']);
$crawledm = mysql_fetch_assoc(mysql_query("SELECT count(crawled) as count FROM domains WHERE crawled != '' AND crawled > '{$m}'"));
$crawledm = $crawledm['count'];
$crawledh = mysql_fetch_assoc(mysql_query("SELECT count(crawled) as count FROM domains WHERE crawled != '' AND crawled > '{$h}'"));
$crawledh = $crawledh['count'];
$crawledd = mysql_fetch_assoc(mysql_query("SELECT count(crawled) as count FROM domains WHERE crawled != '' AND crawled > '{$d}'"));
$crawledd = $crawledd['count'];
$crawledy = mysql_fetch_assoc(mysql_query("SELECT count(crawled) as count FROM domains WHERE crawled != '' AND crawled > '{$y}' AND crawled < '{$d}'"));
$crawledy = $crawledy['count'];
Now, I am using php timestamp (time()) in my columns as a timestamp. I use it because I thought that storing it as integer would be efficient than using datetime.
Now I am having serious problems with selecting time between two timestamps.
For example, I am trying to get records added yesterday (that is between today's date in php timestamp and todaysdate - 24 hours in php timestamp)
It looks like this in sql pseudo code
SELECT * FROM table
WHERE date < phptimestamp(todaysdate) AND date > phptimestamp(todaysdate -24h)
It does get me some records but for some reason the records between this range keep on chaning. The timestamps that I am looking between are static. The records between those time stamps should not be changing because all the records that are added have a currtime stamp.
Real Code
$date = getdate();
$m = time() - (5 * 60);
$h = time() - (($date['minutes'] * 60) + $date['seconds']);
$d = time() - ((60 * 60 * $date['hours']) + ($date['minutes'] * 60) + $date['seconds']);
$y = time() - ((60 * 60 * 24) + ($date['hours'] * 60 * 60) + ($date['minutes'] * 60) + $date['seconds']);
$crawledm = mysql_fetch_assoc(mysql_query("SELECT count(crawled) as count FROM domains WHERE crawled != '' AND crawled > '{$m}'"));
$crawledm = $crawledm['count'];
$crawledh = mysql_fetch_assoc(mysql_query("SELECT count(crawled) as count FROM domains WHERE crawled != '' AND crawled > '{$h}'"));
$crawledh = $crawledh['count'];
$crawledd = mysql_fetch_assoc(mysql_query("SELECT count(crawled) as count FROM domains WHERE crawled != '' AND crawled > '{$d}'"));
$crawledd = $crawledd['count'];
$crawledy = mysql_fetch_assoc(mysql_query("SELECT count(crawled) as count FROM domains WHERE crawled != '' AND crawled > '{$y}' AND crawled < '{$d}'"));
$crawledy = $crawledy['count'];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您在表中使用 TIMESTAMP 列类型,而不是 UNIX 时间戳(它只是一个整数)。
没有理由将日期时间存储为整数 - MySQL(以及所有其他 dbms)经过优化,能够以非常智能的方式处理日期和时间。
假设您将日期存储在 datetime/timestamp 列中,您应该能够使用此查询:
此外,当您在 MySQL 中处理日期和时间时,此文档页面是您的朋友。
I will assume you are using the TIMESTAMP column type in your table, and not a UNIX timestamp (which would just be an integer).
There is no reason to store datetimes as an integer - MySQL (and every other dbms) is optimized to handle dates and times in a very intelligent manner.
Assuming you are storing your dates in datetime/timestamp columns, you should be able to use this query:
Also, as you work with dates and times in MySQL, this doc page is your friend.
将 phptimestamp 替换为 FROM_UNIXTIME 将整数转换为实际时间戳。这应该使您能够比较实际日期而不仅仅是整数。
Replace phptimestamp with FROM_UNIXTIME to convert your integer to a actual timestamp. This should enable you to compare actual dates and not just integers.