【问题标题】:Can't join two tables in one with two unique columns无法使用两个唯一列将两个表合二为一
【发布时间】:2017-01-25 17:36:31
【问题描述】:

我需要选择客户和供应商居住的所有独特的英国城市,从 A 到 Z 排序

我有两张表:CustomersSuppliers。它们都有 CityCountry 列。

这是我的要求:

select distinct Customers.City, Suppliers.City, Customers.Country, Suppliers.Country
from Customers
join Suppliers
on Customers.Country = Suppliers.Country
where Customers.Country like "UK";

结果我得到 4 列:City、City、Country、Country。

我哪里错了?

【问题讨论】:

  • 您得到 4 列,因为您要求 4 列。我不清楚你想要什么。 “向我显示在同一城市和国家/地区也有客户的供应商所在的城市/国家/地区?”
  • 你不想要一个 JOIN 你想要一个 UNION
  • 我需要两个不同表格中的独特英国城市。

标签: mysql sql join


【解决方案1】:

您可以为此使用最小和最大子句。 下面的答案可能对您有所帮助。 MYSQL select DISTINCT values in two columns

【讨论】:

    【解决方案2】:

    谢谢大家。这是有效的正确请求:

    select City,Country
    from Customers
    where Country like "UK"
    union
    select City,Country
    from Suppliers
    where Country like "UK"
    order by City asc;
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多