gpt4 book ai didi

MySQL 查询选择具有最新 created_at 日期的行到列中

转载 作者:行者123 更新时间:2023-11-29 07:16:46 28 4
gpt4 key购买 nike

我正在尝试创建一个查询,我想将我的行转换为行 panel_name GROUP BY 的列,并按个人日期的最后一个 created_at 排列。我的数据以列格式出现,但未显示特定日期。我尝试了多个查询,但没有让我知道结果。这是我的表结构的样子。

+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| panel_name | varchar(191) | YES | | NULL | |
| panel_number | varchar(191) | YES | | NULL | |
| display_from | date | YES | | NULL | |
| open_time | varchar(191) | YES | | NULL | |
| close_time | varchar(191) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+--------------+------------------+------+-----+---------+----------------+

这是我的数据目前的样子:

+-----+-------------------+--------------+--------------+-----------+------------+---------------------+---------------------+---------------------+
| id | panel_name | panel_number | display_from | open_time | close_time | created_at | updated_at | deleted_at |
+-----+-------------------+--------------+--------------+-----------+------------+---------------------+---------------------+---------------------+
| 447 | TARA MUMBAI DAY | 123-1*-*** | NULL | NULL | NULL | 2019-11-13 10:31:27 | 2019-11-13 10:31:27 | NULL |
| 446 | TIME | 123-**-*** | NULL | NULL | NULL | 2019-11-13 10:22:15 | 2019-11-13 10:22:15 | NULL |
| 445 | TIME | ***-**-*** | NULL | NULL | NULL | 2019-11-11 18:00:01 | 2019-11-11 18:33:29 | 2019-11-11 18:33:29 |
| 444 | TIME | ***-**-*** | NULL | NULL | NULL | 2019-11-11 17:59:58 | 2019-11-11 18:33:32 | 2019-11-11 18:33:32 |
| 443 | TARA MUMBAI DAY | 128-74-446 | NULL | NULL | NULL | 2019-11-11 17:51:50 | 2019-11-11 17:51:50 | NULL |
| 442 | TARA MUMBAI DAY | 128-74-446 | NULL | NULL | NULL | 2019-11-11 17:51:30 | 2019-11-11 17:51:30 | NULL |
| 441 | TARA MUMBAI DAY | 128-74-446 | NULL | NULL | NULL | 2019-11-11 17:51:15 | 2019-11-11 17:51:15 | NULL |
| 440 | TARA MUMBAI DAY | 128-74-446 | NULL | NULL | NULL | 2019-11-11 17:50:40 | 2019-11-11 17:50:40 | NULL |
| 439 | TARA MUMBAI DAY | 128-74-*** | NULL | NULL | NULL | 2019-11-11 17:49:17 | 2019-11-11 17:49:17 | NULL |
| 438 | TARA MUMBAI DAY | 128-7-*** | NULL | NULL | NULL | 2019-11-11 17:48:03 | 2019-11-11 17:48:03 | NULL |
| 437 | TARA MUMBAI DAY | 128-7*-*** | NULL | NULL | NULL | 2019-11-11 17:46:22 | 2019-11-11 17:46:22 | NULL |
| 436 | TARA MUMBAI DAY | 128-**-*** | NULL | NULL | NULL | 2019-11-11 17:45:11 | 2019-11-11 17:45:11 | NULL |
| 435 | TIME | 1**-**-*** | NULL | NULL | NULL | 2019-11-11 17:44:43 | 2019-11-11 17:44:43 | NULL |
| 434 | TIME | 1**-**-*** | NULL | NULL | NULL | 2019-11-11 17:43:24 | 2019-11-11 17:43:24 | NULL |
| 433 | TIME | 1**-**-*** | NULL | NULL | NULL | 2019-11-11 17:43:11 | 2019-11-11 17:43:11 | NULL |
| 432 | TARA MUMBAI NIGHT | 700-74-789 | NULL | NULL | NULL | 2019-11-11 17:41:53 | 2019-11-11 17:41:53 | NULL |
| 431 | TARA MUMBAI DAY | 700-74-789 | NULL | NULL | NULL | 2019-11-11 17:41:36 | 2019-11-11 17:41:36 | NULL |
| 430 | MILAN DAY | 700-74-789 | NULL | NULL | NULL | 2019-11-11 17:41:09 | 2019-11-11 17:41:09 | NULL |
| 429 | MILAN DAY | 700-74-789 | NULL | NULL | NULL | 2019-11-11 17:40:52 | 2019-11-11 17:40:52 | NULL |
| 428 | MILAN DAY | 700-74-789 | NULL | NULL | NULL | 2019-11-11 17:40:35 | 2019-11-11 17:40:35 | NULL |
| 427 | MILAN DAY | 700-74-789 | NULL | NULL | NULL | 2019-11-11 17:40:20 | 2019-11-11 17:40:20 | NULL |
| 426 | MILAN DAY | 700-74-789 | NULL | NULL | NULL | 2019-11-11 17:39:06 | 2019-11-11 17:39:06 | NULL |
| 425 | MILAN DAY | 700-74-789 | NULL | NULL | NULL | 2019-11-11 17:38:58 | 2019-11-11 17:38:58 | NULL |
| 424 | MILAN DAY | 700-7*-*** | NULL | NULL | NULL | 2019-11-11 17:34:43 | 2019-11-11 17:34:43 | NULL |
| 423 | MILAN DAY | 700-7*-*** | NULL | NULL | NULL | 2019-11-11 17:34:20 | 2019-11-11 17:34:20 | NULL |
| 422 | MILAN DAY | 700-7*-*** | NULL | NULL | NULL | 2019-11-11 17:33:56 | 2019-11-11 17:33:56 | NULL |
| 421 | TARA MUMBAI DAY | 477-80-244 | NULL | NULL | NULL | 2019-11-11 17:23:18 | 2019-11-11 17:23:18 | NULL |
| 420 | TARA MUMBAI DAY | 477-80-244 | NULL | NULL | NULL | 2019-11-11 17:22:56 | 2019-11-11 17:25:47 | 2019-11-11 17:25:47 |
| 419 | TARA MUMBAI DAY | 477-80-244 | NULL | NULL | NULL | 2019-11-11 17:22:45 | 2019-11-11 17:25:44 | 2019-11-11 17:25:44 |
| 418 | TARA MUMBAI DAY | 477-80-*** | NULL | NULL | NULL | 2019-11-11 17:19:25 | 2019-11-11 17:19:25 | NULL |
+-----+-------------------+--------------+--------------+-----------+------------+---------------------+---------------------+---------------------+

这是我希望看到的结果

 +--+-------------------+------------+-----------+-------------------+------------+
| | TARA MUMBAI DAY | TIME | MILAN DAY | TARA MUMBAI NIGHT | created_at |
+--+-------------------+------------+-----------+-------------------+------------+
123-1*-*** 123-**-*** NULL NULL 2019-11-13
128-74-446 1**-**-*** 700-74-789 700-74-789 2019-11-11

这是到目前为止我已经尝试了多少,但剂量似乎显示了我想要的结果。

SELECT
max(r0.id),
r1.panel_name AS "TIME",
r2.panel_name AS "TARA MUMBAI DAY",
r3.panel_name AS "MILAN DAY",
r4.panel_name AS "TARA MUMBAI NIGHT",
r0.DateOnly
FROM
(SELECT
id, panel_name,panel_number, DATE(created_at) as DateOnly
FROM
foduu_potresult
) r0
LEFT JOIN foduu_potresult r1
ON r1.id = r0.id AND r1.panel_name = 'TIME'
LEFT JOIN foduu_potresult r2
ON r2.id = r0.id AND r2.panel_name = 'TARA MUMBAI DAY'
LEFT JOIN foduu_potresult r3
ON r3.id = r0.id AND r3.panel_name = 'MILAN DAY'
LEFT JOIN foduu_potresult r4
ON r4.id = r0.id AND r4.panel_name = 'TARA MUMBAI NIGHT'
GROUP BY
r0.DateOnly
ORDER BY
r0.id DESC

对这种查询有什么建议吗?我如何才能显示正确的结果。

最佳答案

您可以使用相关子查询来过滤表并为每个 panel_namecreated_at 日期保留具有最新 created_at 的记录.然后,您可以使用条件聚合来转换结果:

select
max(case when panel_name = 'TARA MUMBAI DAY' then panel_number end ) `TARA MUMBAI DAY`,
max(case when panel_name = 'TIME' then panel_number end ) `TIME`,
max(case when panel_name = 'MILAN DAY' then panel_number end ) `MILAN DAY`,
max(case when panel_name = 'TARA MUMBAI NIGHT' then panel_number end ) `TARA MUMBAI NIGHT`,
date(created_at) created_at
from mytable t
where created_at = (
select max(t1.created_at)
from mytable t1
where
date(t1.created_at) = date(t.created_at)
and t1.panel_name = t.panel_name
)
group by date(created_at)

Demo on DB Fiddlde

TARA MUMBAI DAY | TIME       | MILAN DAY  | TARA MUMBAI NIGHT | created_at:-------------- | :--------- | :--------- | :---------------- | :---------128-74-446      | ***-**-*** | 700-74-789 | 700-74-789        | 2019-11-11123-1*-***      | 123-**-*** | null       | null              | 2019-11-13

关于MySQL 查询选择具有最新 created_at 日期的行到列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58834145/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com