1

How to combine many select queries into 1 statement in SQLite?

SELECT * FROM Table1 WHERE condition1
SELECT * FROM Table2 WHERE condition2
SELECT * FROM Table3 WHERE condition3
...
4

2 回答 2

2

Use UNION to combine the results of the three queries.

UNION will remove duplicate leaving on unique rows on the final result. If you want to keep the duplicate rows, use UNION ALL.

SELECT * FROM Table1 WHERE condition1 
UNION
SELECT * FROM Table2 WHERE condition2 
UNION
SELECT * FROM Table3 WHERE condition3

caveat: the number of columns (as well as the data type) must match on each select statement.

UPDATE 1

based on your comment below, You are looking for JOIN,

SELECT  a.*, b.*, c.*
FROM    table1 a
        INNER JOIN table2 b
            ON a.ColName = b.ColName
        INNER JOIN table3 c
            ON a.ColName = c.ColName
-- WHERE    .. add conditions here ..

To further gain more knowledge about joins, kindly visit the link below:

于 2013-03-30T16:18:39.960 回答
1

Your can also query also like SELECT * FROM users where bday > '1970-05-20' UNION ALL SELECT * FROM users where bday < '1970-01-20'

于 2014-05-13T09:55:32.680 回答