gpt4 book ai didi

mysql - 如果在子行中找到匹配,则删除父行,mysql UNION

转载 作者:行者123 更新时间:2023-11-29 06:52:00 25 4
gpt4 key购买 nike

您好,我有两张 table 我们称它们为餐 table 时间表和餐 table 预约

日程表:

+----+
| id |
+----+
| 3 |
| 42 |
+----+

预约表:

+----+-------------+
| id | schedule_id |
+----+-------------+
| 1 | 0 |
| 2 | 42 |
+----+-------------+

我有一个查询,可以从数据库中获取约会和日程安排,它看起来与此类似:

SELECT id FROM appointment 
UNION ALL
SELECT id FROM schedule

我想要获取所有日程安排和约会,如果有一个约会具有匹配的schedule_id,我希望从结果中删除日程安排行

最终结果是

+----+-------------+
| id | schedule_id |
+----+-------------+
| 1 | 0 |
| 2 | 42 |
| 3 | 0 |
+----+-------------+

我需要做什么才能创建该查询?

最佳答案

我建议:

select a.id, a.schedule_id
from appointment a
union all
select s.id, 0
from schedule s
where not exists (select 1 from appointment a where a.id = s.id);

关于mysql - 如果在子行中找到匹配,则删除父行,mysql UNION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47081354/

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