Mysql - How to get the table name where the result came from?

Asked

Viewed 77 times

1

I have the following mysql code:

(SELECT * FROM entrada WHERE idUsuario = "1") UNION 
(SELECT * FROM saida WHERE idUsuario = "1")

It’s all working but I need to know which of the 2 tables the result came from, how do I do it ?

1 answer

4


You can try:

(SELECT 'entrada' as tipo, entrada.* FROM entrada WHERE idUsuario = "1") UNION 
(SELECT 'saida' as tipo, saida.* FROM saida WHERE idUsuario = "1")

Thus, in each record will have a column tipo with the value entrada or saida.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.