简体   繁体   English

如何从两个不同表的列中获取公共值?

[英]How to get the common values from columns of two different tables?

I have two tables customers table and suppliers table.我有两张表customers表和suppliers表。 In both the tables, I have City column with different city names but there may be some cities which are present in both the tables.在这两个表中,我都有具有不同城市名称的City列,但两个表中可能都存在一些城市。 I want to retrieve only those values (city names) which are present in both.我只想检索两者中都存在的那些值(城市名称)。 How can I get those values?我怎样才能得到这些值?

I suggest to INNER JOIN two SELECT statements.我建议 INNER JOIN 两个 SELECT 语句。 Attention on the alias!注意别名!

select distinct city from 
(select city from customers) c
inner join
(select city from suppliers) s
on c.city = s.city;

You need to use the inner join, something like this你需要使用内部连接,像这样

select customers.name, customers.city, suppliers.address
from customers
inner join suppliers on customers.city = suppliers.city

MySQL INNER JOIN Keyword The INNER JOIN keyword selects records that have matching values in both tables. MySQL INNER JOIN 关键字 INNER JOIN 关键字选择在两个表中具有匹配值的记录。

MySQL INNER JOIN Keyword MySQL INNER JOIN 关键字

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

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