开发者

How to differenciate the data when doing a UNION on 2 SELECTS?

If I have the following two tables :

Employes

Bob

Gina

John

Customers

开发者_StackOverflow中文版

Sandra

Pete

Mom

I will do a UNION for having :

Everyone

Bob

Gina

John

Sandra

Pete

Mom

The question is : In my result, how can I creat a dumn column of differenciate the data from my tables ?

Everyone

Bob (Emp)

Gina (Emp)

John (Emp

Sandra (Cus)

Pete (Cus)

Mom (Cus)

I want to know from with table the entry is from withouth adding a new column in the database...

SELECT Employes.name
FROM Employes
UNION
SELECT Customers.name
FROM Customers;


Just introduce a constant addition to the column (rather than the table):

SELECT name || ' (Emp)' as name FROM Employees
UNION
SELECT name || ' (Cus)' as name FROM Customers;

and spell "Employees" correctly :-)


Add the "column" in your select statement.

SELECT Employes.name, '(Emp)' as PersonType
FROM Employes
UNION
SELECT Customers.name, '(Cus)' as PersonType
FROM Customers;


SELECT 'Employee' AS table
       Employes.name 
  FROM Employes 
UNION 
SELECT 'Customer' AS table
       Customers.name 
  FROM Customers;

or

SELECT Employes.name + ' (Emp)'
FROM Employes 
UNION 
SELECT Customers.name + ' (Cus)'
FROM Customers; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜