Removing duplicate results while using UNION SELECT

前端 未结 2 1482
梦谈多话
梦谈多话 2021-01-12 03:57

Ive got a problem with a MySQL query.

Let\'s say, we have two tables:

id qty

1......1

2......1

3......1

4......3

相关标签:
2条回答
  • 2021-01-12 04:43

    GROUP BY didnt help either

    Really? Did you try like this?

    SELECT id, MAX(qty) AS qty
    FROM
    (
        SELECT id, qty FROM table1
        UNION ALL
        SELECT id, qty FROM table2
    ) T1
    GROUP BY id
    
    0 讨论(0)
  • 2021-01-12 04:45

    You can use UNION DISTINCT. Maybe it runs...

    SELECT * FROM t1 WHERE ...
    
    UNION DISTINCT
    
    SELECT * FROM t2 WHERE ...
    
    0 讨论(0)
提交回复
热议问题