Hosam alzagh Hosam alzagh - 7 months ago 5
SQL Question

Discrimination results from tables use UNION

i get result with UNION put i want know result form any table

SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;


and result is this


city
572
123



But I'd like this result:

But I'd like this result:


city tablet_result
572 Customers
123
Suppliers

Answer

Ok, try:

SELECT City,'Customers' as tblName FROM Customers
UNION
SELECT City,'Suppliers' as tblName FROM Suppliers
ORDER BY City;

Play with the order by.

Also consider doing a UNION ALL. See this for the difference.