Php-MySql查询,根据日期时间选择数据

发布于 2024-12-24 18:59:23 字数 187 浏览 2 评论 0原文

我想获取具有相同日期的行数。但一秒钟肯定是可以忽略不计的。

例如 ; 2012-01-03 13:12:28 和 2012-01-03 13:12:27 应该被视为相同。

(我的表名称是 myTable ,日期时间列名称是 date

我希望你能帮助我..

I would like to get number of rows that have same date. But one second must be negligible.

For example ; 2012-01-03 13:12:28 and 2012-01-03 13:12:27 should be perceived as the same.

( I have a table name is myTable and the datetime column name is date )

I hope you will help me..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

慕巷 2024-12-31 18:59:23

它很简单:

SELECT * FROM myTable WHERE date >= '2012-01-03 13:12:27' AND date <= '2012-01-03 13:12:28';

如果您想从变量构建它,您可以这样做:

$date = '2012-01-03 13:12:27';
$timestamp = strtotime($date);
$mysqli = new mysqli();

// Note: allowing 1 second either side, this is up to you
$lower = date('Y-m-d H:i:s', $timestamp - 1);
$upper = date('Y-m-d H:i:s', $timestamp + 1);

$stmt = $mysqli->prepare('SELECT * FROM myTable WHERE date >= ? AND date <= ?');
$stmt->bind_param('ss', $lower, $upper);
$stmt->execute();

编辑:根据下面的评论,以下 SQL 应该返回您想要的内容。请注意,如果日期包含“2012-01-03 13:12:28”,则 :27 和 :28 都会被计入。

SELECT
    upper_date,
    COALESCE(lower_count, 0) + upper_count
FROM
    (SELECT
        date AS upper_date,
        COUNT(1) AS upper_count
    FROM myTable
    GROUP BY date
    ) AS upper
LEFT JOIN
    (SELECT
        date - INTERVAL 1 SECOND AS lower_date,
        COUNT(1) AS lower_count
    FROM myTable
    GROUP BY date
    ) AS lower
ON upper.upper_date = lower.lower_date

It's as simple as:

SELECT * FROM myTable WHERE date >= '2012-01-03 13:12:27' AND date <= '2012-01-03 13:12:28';

If you want to build it up from a variable, you could do this:

$date = '2012-01-03 13:12:27';
$timestamp = strtotime($date);
$mysqli = new mysqli();

// Note: allowing 1 second either side, this is up to you
$lower = date('Y-m-d H:i:s', $timestamp - 1);
$upper = date('Y-m-d H:i:s', $timestamp + 1);

$stmt = $mysqli->prepare('SELECT * FROM myTable WHERE date >= ? AND date <= ?');
$stmt->bind_param('ss', $lower, $upper);
$stmt->execute();

EDIT: As per your comment below, the following SQL should return what you're after. Note that if a date contains '2012-01-03 13:12:28' it will be counted for both :27 and :28.

SELECT
    upper_date,
    COALESCE(lower_count, 0) + upper_count
FROM
    (SELECT
        date AS upper_date,
        COUNT(1) AS upper_count
    FROM myTable
    GROUP BY date
    ) AS upper
LEFT JOIN
    (SELECT
        date - INTERVAL 1 SECOND AS lower_date,
        COUNT(1) AS lower_count
    FROM myTable
    GROUP BY date
    ) AS lower
ON upper.upper_date = lower.lower_date
木落 2024-12-31 18:59:23
SELECT p1.date, p1.sex, p2.date, p2.sex, p1.species ......
        FROM table_name AS p1, table_name AS p2
        WHERE p1.date-p2.species in(0,1);

对于这种情况,我们使用带有别名的表的自连接...并且由于您的条件是日期之间的差异不应超过 1,所以...

我假设您已将日期存储为 mysql datetime...执行“-”操作..

SELECT p1.date, p1.sex, p2.date, p2.sex, p1.species ......
        FROM table_name AS p1, table_name AS p2
        WHERE p1.date-p2.species in(0,1);

For such cases we use self join of a table with aliases...and as your condition is the difference between the dates should not be more than 1 here it goes...

I presume that you have stored date as mysql datetime...to perform "-" operation..

梦回梦里 2024-12-31 18:59:23

尝试一次:

SELECT count(*)
FROM myTable
WHERE DATE_FORMAT(date, '%Y-%m-%d %H:%i') = '2012-01-03 13:12';

或者对于与组相关的 COUNT:

SELECT DATE_FORMAT(date, '%Y-%m-%d %H:%i') as myDate, count(*)
FROM myTable
GROUP BY myDate;

Try this once:

SELECT count(*)
FROM myTable
WHERE DATE_FORMAT(date, '%Y-%m-%d %H:%i') = '2012-01-03 13:12';

Or for a group-related COUNT:

SELECT DATE_FORMAT(date, '%Y-%m-%d %H:%i') as myDate, count(*)
FROM myTable
GROUP BY myDate;
半世蒼涼 2024-12-31 18:59:23

如果您只需要计算每个日期的行数,那么这很简单 GROUP BY:

SELECT COUNT(*) as cnt, DATE_FORMAT(date, '%Y-%m-%d') as my_date
  FROM myTable
  GROUP BY my_date

但是根据您的问题,您不仅需要计算日期内的行数,而且还不需要计算单个事件,而是计算一系列事件。在这种情况下,您需要定义系列到底是什么。假设一秒内发生的两个事件应算作一个。如果有四个彼此距离为 0.7 的事件怎么办?在这种情况下,我建议分两步计算系列。

首先 - 按时间排序事件:

SELECT * FROM myTable WHERE date >= XXXX and date < YYYY ORDER BY date ASC

然后在应用程序端,您可以计算两个记录之间的间隔,如果两个关闭事件之间的距离超过一秒,则增加计数器。

不要忘记在 date 字段上添加索引以加快此查询速度。

If you only need to count number of rows per date it would be simple GROUP BY:

SELECT COUNT(*) as cnt, DATE_FORMAT(date, '%Y-%m-%d') as my_date
  FROM myTable
  GROUP BY my_date

But by your question you are not only need to count rows within a date, but also count not a single events but it's series. In this case you will need to define what exactly series are. Say two events within one second should be counted as one. What if there are four events with 0.7 distance between each other? In this case I would suggest to count series in two steps.

First - order events by time:

SELECT * FROM myTable WHERE date >= XXXX and date < YYYY ORDER BY date ASC

Then on the application side you can count the gap between two records and increase counter if distance between two close events is more than one second.

Don't forget to add index on date field to speed up this query.

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