我的 mysql 中有两个表,我想根据对两个表的组合查询来提取结果。我试过加入和内部加入但没有成功
的结构表A是
id userid topic
1 34 love
3 64 friendship
35 574 romance
32 253 games
95 633 football
54 26 cricket
648 63 music
表B是
id location username
34 Australia krkrff
64 india dieiei
574 pakistan frkfrf
253 japan frfffrk
633 india ifirf
26 Australia riiri
63 Australia frffjrr
请注意,tableA 中的 userid 和 TableB 中的 id 相同。两者都反射(reflect)了相同用户的数据。我想通过从 tableB 中过滤位置列来显示 tableA 数据。假设我想显示 tableB 的主题并且用户属于澳大利亚那么它应该给出输出:love 蟋蟀 音乐
你可以在表 B 中看到 34,26 和 63 属于澳大利亚,所以输出就是这样。如果位置是印度,那么输出将是
友谊和足球。请告诉如何写sql查询。
最佳答案
以下应选择您所描述的内容:
select a.topic
from tableA a
join tableB b on b.id = a.userid
where b.location = 'Australia' -- or whichever location you filter on
相当于:
select a.topic
from tableA a
join tableB b on b.id = a.userid and b.location = 'Australia'
关于mysql查询连接/内部连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10060325/