mysql 查询连接/内部连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10060325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:52:29  来源:igfitidea点击:

mysql query join/inner join

mysqljoinleft-joininner-join

提问by Steeve

i have two tables in my mysql i want to extract the results based on combined query for both tables. i tried join as well as inner join but no success the structure of

我的 mysql 中有两个表,我想根据两个表的组合查询提取结果。我尝试加入以及内部加入但没有成功的结构

tableA is

表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

tableB is

表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

Please note that in tableA userid and in TableB id is same .both reflect the data of same users.i want to display tableA data by filtering location column from tableB. suppose that i want to display topic of tableB and the users belongs to Australia then it should give output :love cricket music

请注意 tableA 中的 userid 和 TableB 中的 id 是相同的。两者都反映了相同用户的数据。我想通过过滤 tableB 中的位置列来显示 tableA 数据。假设我想显示 tableB 的主题并且用户属于澳大利亚,那么它应该给出输出:love cricket music

you can see in tableB that 34,26 & 63 belongs to Australia so the output is like that. if the location is india then outpput will be

您可以在 tableB 中看到 34,26 & 63 属于澳大利亚,因此输出是这样的。如果位置是印度,那么输出将是

friendship and football.please tell how to write sql query.

友谊和足球。请告诉如何编写sql查询。

回答by Paul Bellora

The following should select what you're describing:

以下应选择您所描述的内容:

select a.topic
from tableA a
join tableB b on b.id = a.userid
where b.location = 'Australia' -- or whichever location you filter on

which is equivalent to:

这相当于:

select a.topic
from tableA a
join tableB b on b.id = a.userid and b.location = 'Australia'

回答by Gabriel Santos

SELECT a.topic, b.topic FROM tableA a, tableB b WHERE a.id = b.id AND b.location = 'Australia'

回答by Rohan Durve

Could you please elaborate on what you mean by "tableA userid and in TableB id is same", because they are visible different such that we cannot use them as keys.

您能否详细说明“tableA 用户 ID 和 TableB 中的 ID 相同”是什么意思,因为它们的可见性不同,因此我们不能将它们用作键。

As I understand it however, you'd prolly want to do a JOIN and then a SELECT only for the TOPIC.

但是,据我了解,您肯定会想要对 TOPIC 执行 JOIN 和 SELECT 操作。

So, your query should be:

所以,你的查询应该是:

SELECT t1.topic from table1 t1 JOIN tableB t2 on t2.id = t1.id WHERE t2.location = 'Australia'

回答by Mohammad Saberi

Try this:

尝试这个:

SELECT tableA.topic FROM tableA JOIN tableB
ON tableA.userid = tableB.id
WHERE tableB.location = 'Australia';