SQLite Forum

Multiply table select with table identification
Login

Multiply table select with table identification

(1) By FalloutST on 2021-11-25 13:04:38 [source]

Is it possible to print table name in result, and how if it is...
in example:

SELECT * FROM TABLE1, TABLE2;
i'd get result
1 | 2 | 3 ---- from TABLE1
4 | 5 | 6 ---- from TABLE2

how to print 

1 | 2 | 3 | TABLE1
4 | 5 | 6 | TABLE2

(2) By Stephan Beal (stephan) on 2021-11-25 14:19:11 in reply to 1 [link] [source]

SELECT * FROM TABLE1, TABLE2; i'd get result ...

Those are not the results you'd get from that query. You'd get a number of results equal to the number of rows from table 1 multiplied by the number of rows from table 2:

sqlite> create temp table x(y);
sqlite> create temp table z(a);
sqlite> insert into x(y) values(1),(2),(3);
sqlite> insert into z(a) values(4),(5),(6);
sqlite> select * from x, z;
1,4
1,5
1,6
2,4
2,5
2,6
3,4
3,5
3,6

What you want is something like:

sqlite> select *, 'table x' from x union select *, 'table z' from z;
1,'table x'
2,'table x'
3,'table x'
4,'table z'
5,'table z'
6,'table z'