简体   繁体   中英

MySQL select uniques from two different tables?

I have two different tables, and I want to get the uniques as if they where a single table. The table structures are the same. I have tried UNION , UNION DISTINCT , SELECT DISTINCT with a UNION , and all kinds of things. But I am still getting duplicates and stuff.

Is there anyways to get the unique email address along with other data

SELECT DISTINCT email_address, first_name, last_name,
    street_address, city, postal_code, province, created_date
FROM entries
WHERE subscribe='1'

That gets it from one table.

Note, if I just wanted the email address, UNION works. But when I add the other data it doesn't do anything anymore.

Try this

SELECT email_address, first_name, last_name,
    street_address, city, postal_code, province, created_date
FROM 
    (SELECT * FROM entries WHERE subscribe='1'
    UNION
    SELECT * FROM entries2 WHERE subscribe='1') AS tmp
GROUP BY email_address
    (SELECT /*Fields*/ FROM entries e1)
    UNION
    (SELECT /*same Fields*/ FROM entries2 e2)
    WHERE subscribe=1
    GROUP BY email_address

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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