Databases Querying Relational Databases Set Operations Union Operations

Gia Hoang Nguyen
Gia Hoang Nguyen
4,137 Points

How to reset the MakeID with the correct order ID?

Select ForeignMakeID As ID, MakeName As GlobalMake From ForeignMake Union Select MakeID, MakeName From Make;

This will return all Foreign and Domestic Make but the ID has some duplicate number.

Is it possible to set the MakeID in the correct order without having any duplicate?

2 Answers

Steven Parker
Steven Parker
177,847 Points

From the database perspective, these aren't duplicates because either the name or number is different. If you want to display all the make names without duplicates, just leave off the id number:

SELECT MakeName AS GlobalMake FROM ForeignMake UNION SELECT MakeName FROM Make;

But with a subquery, you could combine the ID's into one column and use grouping to eliminate the duplicates:

SELECT group_concat(ID, ", ") AS IDs, GlobalMake FROM
(SELECT ForeignMakeID||"F" AS ID, MakeName AS GlobalMake FROM ForeignMake
 UNION ALL SELECT MakeID||"M", MakeName FROM Make)
GROUP BY GlobalMake

Since the ID's are associated with a particular table, I concatenated a letter onto each one to identify which table it applies to.