gpt4 book ai didi

MySQL - 从 2 个表中选择结果中的几列应基于条件语句

转载 作者:行者123 更新时间:2023-11-30 21:42:36 25 4
gpt4 key购买 nike

尊敬的 MySQL/SQL 专家,

在以下场景中请求您的帮助。我有 2 个表,如下所述。

TABLE1
+----+---------------------+---------------------+----------------+----------------+
|uid | active_hour_start | active_hour_end | activity_name | activity_value |
+----+---------------------+---------------------+----------------+----------------+
| 01 | 2018-06-17 13:00:00 | 2018-06-17 14:00:00 | activity1 | 10 |
| 01 | 2018-06-17 13:00:00 | 2018-06-17 14:00:00 | activity2 | 15 |
| 01 | 2018-06-17 12:00:00 | 2018-06-17 13:00:00 | activity1 | 20 |
| 01 | 2018-06-17 12:00:00 | 2018-06-17 13:00:00 | activity2 | 30 |
| 01 | 2018-06-17 12:00:00 | 2018-06-17 13:00:00 | activity3 | 05 |
| 01 | 2018-06-17 11:00:00 | 2018-06-17 12:00:00 | activity4 | 55 |
| 01 | 2018-06-17 10:00:00 | 2018-06-17 11:00:00 | activity5 | 30 |
| 01 | 2018-06-17 10:00:00 | 2018-06-17 11:00:00 | activity1 | 25 |
| 01 | 2018-06-17 09:00:00 | 2018-06-17 10:00:00 | activity4 | 15 |
| 01 | 2018-06-17 09:00:00 | 2018-06-17 10:00:00 | activity5 | 35 |
+----+-------+---------+------------------+-------+---------------+----------------+

TABLE2
+----+---------------+---------------------+------------+-----------------+
|uid | user_name | start_date_time | parameter | parameter_value |
+----+---------------+---------------------+------------+-----------------+
| 01 | s01234 | 2018-06-17 11:35:26 | status | B |
| 01 | s01234 | 2018-06-17 11:35:26 | type | GOLD |
| 01 | s01234 | 2018-06-16 08:15:36 | status | X |
| 01 | s01234 | 2018-06-16 08:45:36 | type | SILVER |
| 02 | b23456 | 2018-06-16 13:00:00 | status | A |
| 02 | b23456 | 2018-06-16 13:00:00 | type | SILVER |
+----+---------------+---------------------+------------+-----------------+

正如您在表 2 中看到的,每次参数值更改时都会维护一条记录。我想从 2 个表中返回结果集,如下所示:

EXPECTED RESULTSET
+----+---------------------+---------------------+----------------+----------------+----------------+----------------+
|uid | active_hour_start | active_hour_end | activity_name | activity_value | status | type |
+----+---------------------+---------------------+----------------+----------------+----------------+----------------+
| 01 | 2018-06-17 13:00:00 | 2018-06-17 14:00:00 | activity1 | 10 | B | GOLD |
| 01 | 2018-06-17 13:00:00 | 2018-06-17 14:00:00 | activity2 | 15 | B | GOLD |
| 01 | 2018-06-17 12:00:00 | 2018-06-17 13:00:00 | activity1 | 20 | B | GOLD |
| 01 | 2018-06-17 12:00:00 | 2018-06-17 13:00:00 | activity2 | 30 | B | GOLD |
| 01 | 2018-06-17 12:00:00 | 2018-06-17 13:00:00 | activity3 | 05 | B | GOLD |
| 01 | 2018-06-17 11:00:00 | 2018-06-17 12:00:00 | activity4 | 55 | X | SILVER |
| 01 | 2018-06-17 10:00:00 | 2018-06-17 11:00:00 | activity5 | 30 | X | SILVER |
| 01 | 2018-06-17 10:00:00 | 2018-06-17 11:00:00 | activity1 | 25 | X | SILVER |
| 01 | 2018-06-17 09:00:00 | 2018-06-17 10:00:00 | activity4 | 15 | X | SILVER |
| 01 | 2018-06-17 09:00:00 | 2018-06-17 10:00:00 | activity5 | 35 | X | SILVER |
+----+-------+---------+------------------+-------+---------------+----------------+----------------+----------------+

结果集包含 TABLE1 中给定持续时间的所有行(基于 active_hour_start/active_hour_end 上的 where 子句)以及 2 个额外的列,这些列包含基于 start_date_time 的 TABLE 2 中的参数名称和值。

谢谢。

最佳答案

以下查询将返回所需的结果。

select
table1.uid,table1.active_hour_end,table1.activity_name,
table1.activity_name,tab2.status,tab2.type
from
table1
inner join
(
SELECT
A.uid,
A.username,
A.start_date_time,
St.Status,
t.type
from
(
select distinct
uid,
user_name,
start_date_time
from
table2
)
A
INNER JOIN
(
select
uid,
start_date_time,
parameter_value as Status
from
table2
where
parameter = 'status'
)
ST
on (A.uid = ST.uid
and A.start_date_time = st.start_date_time )
INNER JOIN
(
select
uid,
start_date_time,
parameter_value as type
from
table2
where
parameter = 'type'
)
T
on (A.uid = T.uid
and A.start_date_time = T.start_date_time )
)
tab2
on (table1.uid = tab2.uid
and tab2.start_date_time >= table1.active_hour_start
and start_date_time <= active_hour_end)

关于MySQL - 从 2 个表中选择结果中的几列应基于条件语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50902585/

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