0

Here are the results with UNION, but I also want to know the source table associated with each row:

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

The actual result of the query is this:

city
572
123

But I'd like this result instead:

city tablet_result
572 Customers
123 Suppliers

Leigh
  • 28,765
  • 10
  • 55
  • 103
xpredo
  • 1,282
  • 17
  • 27

2 Answers2

3
SELECT City, 'Customers' as tablet_result FROM Customers
UNION
SELECT City, 'Suppliers' as tablet_result FROM Suppliers
yefrem
  • 666
  • 7
  • 20
2

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.

Community
  • 1
  • 1
Drew
  • 24,851
  • 10
  • 43
  • 78