how to select distinct value from multiple tables

前端 未结 2 935
-上瘾入骨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   ║
    ╚════════╝
    

提交回复
热议问题