简体   繁体   English

MySQL --- 从多个表中进行选择的显式 INNER JOIN

[英]MySQL --- Explicit INNER JOIN with selection from multiple tables

Whenever I have a need to do an inner join where I have to select columns from multiple tables, I have got the syntax working correctly with implicit joins, without any difficulty.每当我需要进行内部联接时,我必须从多个表中选择列,我就可以使用隐式联接使语法正常工作,没有任何困难。 However, I have struggled to get it working with explicit inner joins.但是,我一直在努力让它与显式内部连接一起工作。

Let me illustrate with an example from the MySQL World Database让我用MySQL World Database 中的一个例子来说明

My illustrative query, using implicit inner join , is as follows:我使用隐式内部连接的说明性查询如下:

SELECT Name, Language, Percentage
FROM Country, CountryLanguage WHERE Code = CountryCode ;

This works as expected.这按预期工作。 I can have the columns in any order, from either table, without any issues.我可以从任一表中按任何顺序排列列,没有任何问题。

I would like to have the explicit INNER JOIN version of the above query (using "INNER JOIN" and "ON").我想要上述查询的显式 INNER JOIN 版本(使用“INNER JOIN”和“ON”)。

You can simply replace the , in your implicit join with the word JOIN :您可以简单地将隐式连接中的,替换为JOIN

SELECT Name, Language, Percentage
FROM Country
JOIN CountryLanguage 
WHERE Code = CountryCode

and the query will work fine.并且查询将正常工作。 You can also replace WHERE with ON and again it will work fine.你也可以用ON替换WHERE ,它会再次正常工作。 Finally if you want to explicitly name the tables where the columns come from (and this is the preferred approach), you would use:最后,如果您想明确命名列来自的表(这是首选方法),您可以使用:

SELECT c.Name, cl.Language, cl.Percentage
FROM Country c
JOIN CountryLanguage cl
ON c.Code = cl.CountryCode

Maybe it would be like也许它会像

SELECT Name, Language, Percentage, 
FROM Country
INNER JOIN CountryLanguage ON Country.Code = CountryLanguage.CountryCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM