我有一个表,其中包含带有日期列的数据。需要Mysql查询帮助!

发布于 2024-10-09 19:15:22 字数 1460 浏览 0 评论 0原文

现在,我在我的列中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不疑不惑不回忆 2024-10-16 19:15:22

我假设您在表中使用 TIMESTAMP 列类型,而不是 UNIX 时间戳(它只是一个整数)。

没有理由将日期时间存储为整数 - MySQL(以及所有其他 dbms)经过优化,能够以非常智能的方式处理日期和时间。

假设您将日期存储在 datetime/timestamp 列中,您应该能够使用此查询:

SELECT *
FROM `table`
WHERE `date` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()

此外,当您在 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:

SELECT *
FROM `table`
WHERE `date` BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()

Also, as you work with dates and times in MySQL, this doc page is your friend.

删除→记忆 2024-10-16 19:15:22

将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文