获取开始和结束时间 MYSQL
Getting The Start and End Time MYSQL
我正在尝试检索可以确定开始和结束日期的数据并查看 petname 的变化间隔
这是我的示例数据。
这是我的预期输出
我试过这样查询
SELECT
`line`, `Petname`, MIN(`start date`), MAX(`end date`)
FROM
`tablename`
WHERE
timestamp BETWEEN '2020-07-01 00:00:00' AND NOW() group by petname
但我的输出 return 是这样的
我没有得到你 table 的创建 sql,因此我创建了一个只是为了测试。
以下是我的验证table。可能和你的不一样,请根据自己的情况修改table.
+----+---------+------------+------------+
| id | petname | start_date | end_date |
+----+---------+------------+------------+
| 1 | FKN92B | 2020-07-01 | 2020-07-02 |
| 2 | FKN92B | 2020-07-02 | 2020-07-04 |
| 3 | FKN8LD | 2020-07-04 | 2020-07-05 |
| 4 | FKN8LD | 2020-07-07 | 2020-07-08 |
| 5 | FKN92B | 2020-07-11 | 2020-07-12 |
| 6 | FKN92B | 2020-07-12 | 2020-07-14 |
| 7 | FKN8LD | 2020-07-08 | 2020-07-09 |
| 8 | FKN8LD | 2020-07-11 | 2020-07-11 |
+----+---------+------------+------------+
8 rows in set (0.00 sec)
SQL如下,请试一试,如有问题请告诉我:
mysql> SET @lastpetname := null;
Query OK, 0 rows affected (0.00 sec)
mysql> SET @groupnum := 0;
Query OK, 0 rows affected (0.00 sec)
mysql> select petname, min(start_date), max(end_date)
-> from
-> (SELECT
-> case
-> when @lastpetname is null then @groupnum := @groupnum + 1
-> when @lastpetname != petname > 0 then @groupnum := @groupnum + 1
-> else @groupnum := @groupnum
-> end as group_num,
-> @lastpetname := petname as petname,
-> `start_date`,
-> `end_date`
-> from `petlog`) t
-> group by group_num;
+---------+-----------------+---------------+
| petname | min(start_date) | max(end_date) |
+---------+-----------------+---------------+
| FKN92B | 2020-07-01 | 2020-07-04 |
| FKN8LD | 2020-07-04 | 2020-07-08 |
| FKN92B | 2020-07-11 | 2020-07-14 |
| FKN8LD | 2020-07-08 | 2020-07-11 |
+---------+-----------------+---------------+
4 rows in set (0.00 sec)
我正在尝试检索可以确定开始和结束日期的数据并查看 petname 的变化间隔
这是我的示例数据。
这是我的预期输出
我试过这样查询
SELECT
`line`, `Petname`, MIN(`start date`), MAX(`end date`)
FROM
`tablename`
WHERE
timestamp BETWEEN '2020-07-01 00:00:00' AND NOW() group by petname
但我的输出 return 是这样的
我没有得到你 table 的创建 sql,因此我创建了一个只是为了测试。 以下是我的验证table。可能和你的不一样,请根据自己的情况修改table.
+----+---------+------------+------------+
| id | petname | start_date | end_date |
+----+---------+------------+------------+
| 1 | FKN92B | 2020-07-01 | 2020-07-02 |
| 2 | FKN92B | 2020-07-02 | 2020-07-04 |
| 3 | FKN8LD | 2020-07-04 | 2020-07-05 |
| 4 | FKN8LD | 2020-07-07 | 2020-07-08 |
| 5 | FKN92B | 2020-07-11 | 2020-07-12 |
| 6 | FKN92B | 2020-07-12 | 2020-07-14 |
| 7 | FKN8LD | 2020-07-08 | 2020-07-09 |
| 8 | FKN8LD | 2020-07-11 | 2020-07-11 |
+----+---------+------------+------------+
8 rows in set (0.00 sec)
SQL如下,请试一试,如有问题请告诉我:
mysql> SET @lastpetname := null;
Query OK, 0 rows affected (0.00 sec)
mysql> SET @groupnum := 0;
Query OK, 0 rows affected (0.00 sec)
mysql> select petname, min(start_date), max(end_date)
-> from
-> (SELECT
-> case
-> when @lastpetname is null then @groupnum := @groupnum + 1
-> when @lastpetname != petname > 0 then @groupnum := @groupnum + 1
-> else @groupnum := @groupnum
-> end as group_num,
-> @lastpetname := petname as petname,
-> `start_date`,
-> `end_date`
-> from `petlog`) t
-> group by group_num;
+---------+-----------------+---------------+
| petname | min(start_date) | max(end_date) |
+---------+-----------------+---------------+
| FKN92B | 2020-07-01 | 2020-07-04 |
| FKN8LD | 2020-07-04 | 2020-07-08 |
| FKN92B | 2020-07-11 | 2020-07-14 |
| FKN8LD | 2020-07-08 | 2020-07-11 |
+---------+-----------------+---------------+
4 rows in set (0.00 sec)