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,605
  • 10
  • 52
  • 98
Hosam Elzagh
  • 1,032
  • 14
  • 25

2 Answers2

2
SELECT City, 'Customers' as tablet_result FROM Customers
UNION
SELECT City, 'Suppliers' as tablet_result FROM Suppliers
yefrem
  • 656
  • 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,556
  • 10
  • 41
  • 75