简体   繁体   中英

How to obtain row with a column name that exists in two tables which is not unique?

I have two tables, vendors and customers , with the following fields:

vendors:

vendor_id         name                  UUID
---------    --------------          ---------------
1               V1 vendor               01ffd02
2               V2 vendor               02daaa2
3               V3 vendor               41ddasa

customer:

customer_id         name                  UUID
---------    --------------          ---------------
1               cust1                  71ffd02
2               cust2                  92daaa2
3               cust3                  11ddasa

The UUIDs above are not foreign keys, and not unique for both the tables.

I have to write a single query to capture all rows in either table with a given UUID. For example, if UUID = '11ddasa' , the result should be the last row in customer above.

SELECT  *
FROM    vendors
WHERE   uuid = '11ddasa'
UNION ALL
SELECT  *
FROM    customers
WHERE   uuid = '11ddasa'

Is this what you're looking for?

SELECT name,UUID from vendors where UUID='11ddasa'
UNION
SELECT name,UUID from customer where UUID='11ddasa'

This will return all rows from either table with UUID.

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