Find out which table the data is from, in a Union

Asked

Viewed 466 times

5

Hello, I have a php system, using codeigniter, and in it runs the following command:

$data['dados_tabelas'] = Tabela1::find_by_sql('SELECT * FROM tabela1) UNION (Select * from tabela2)');

That’s about the size of it. I wanted to know if there is any way I can find out which data came from which table, because I want to bring the Tabela1 data of one color and the table data of another color. Thank you

2 answers

5


You can add an identifier in select and then make an if in your code

$data['dados_tabelas'] = Tabela1::find_by_sql("SELECT 'tbl1' as identificador,* FROM tabela1) UNION (Select 'tbl2' as identificador,* from tabela2)");

2

as far as I know does not give but you can bring the table name within the result of each row, something like this:

SELECT
    id,
    batata,
    outro_campo,
    'tabela1'
FROM
    tabela1
UNION
SELECT
    id,
    pao_de,
    nome,
    'tabela2'
FROM
    tabela2

Browser other questions tagged

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