how to select distinct value from multiple tables

前端 未结 2 934
-上瘾入骨i
-上瘾入骨i 2021-02-05 02:39

I need to get distinct values from 3 tables.

When I perform this code:

select DISTINCT(city) from a,b,c 

I get an error which says that

相关标签:
2条回答
  • 2021-02-05 03:17

    The UNION keyword will return unique records on the result list. When specifying ALL (UNION ALL) will keep duplicates on the result set, which the OP don't want.

    SELECT city FROM tableA
    UNION
    SELECT city FROM tableB
    UNION
    SELECT city FROM tableC
    
    • SQLFiddle Demo

    RESULT

    ╔════════╗
    ║  CITY  ║
    ╠════════╣
    ║ Krakow ║
    ║ Paris  ║
    ║ Rome   ║
    ║ London ║
    ║ Oslo   ║
    ╚════════╝
    
    0 讨论(0)
  • 2021-02-05 03:25
    SELECT city FROM A
    UNION DISTINCT
    SELECT city FROM B
    UNION DISTINCT
    SELECT city FROM C
    
    0 讨论(0)
提交回复
热议问题