Discover from which table the data is in a union

5

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

$data['dados_tabelas'] = Tabela1::find_by_sql('SELECT * FROM tabela1) UNION (Select * from tabela2)');
More or less. I wanted to know if I have to find out what data came from which table, because I want to bring the data of table1 of one color and those of table2 of another color into view. Thanks

    
asked by anonymous 18.06.2015 / 20:52

2 answers

5

You can add an identifier in the 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)");
    
18.06.2015 / 20:55
2

As far as I know, you can not but you can bring the table name into the result of each line, something like this:

SELECT
    id,
    batata,
    outro_campo,
    'tabela1'
FROM
    tabela1
UNION
SELECT
    id,
    pao_de,
    nome,
    'tabela2'
FROM
    tabela2
    
18.06.2015 / 20:57