gpt4 book ai didi

mysql - mysql 中的条件连接表

转载 作者:行者123 更新时间:2023-11-29 05:49:14 25 4
gpt4 key购买 nike

我有两张 table 。我想要列出这些表中 guest_id 1 允许和不允许的所有事件。

第一个表

activity_name_id | activity_name |

1 Entry

2 Exit

3 Break

第二个表

allowed_guest_id | allowed_activity_name_id |
1 1
2 1
1 3
2 4

现在我想要结果

activity_name_id | activity_name |allowed_guest_id | allowed_activity_name_id |

1 Entry 1 1

2 Exit null 2

3 Break 1 3

我已经尝试过了。但它只返回两行。第一个和最后一个。

select * from table_activity_name
left join table_allowed_activity
on activity_name_id = allowed_activity_name_id
where allowed_activity_guest_id = 1;

最佳答案

试试这个

select ta.activity_name_id, ta.activity_name, tal.allowed_guest_id, tal.allowed_activity_name_id from table_activity_name as ta
left join table_allowed_activity as tal
on ta.activity_name_id = tal.allowed_activity_name_id
and tal.allowed_activity_guest_id = 1;

或IN操作

select ta.activity_name_id, ta.activity_name, tal.allowed_guest_id, tal.allowed_activity_name_id from table_activity_name as ta
left join table_allowed_activity as tal
on ta.activity_name_id = tal.allowed_activity_name_id
and tal.allowed_activity_guest_id in (1,2);

关于mysql - mysql 中的条件连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56002339/

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