table1
ID
SUBJECT
CONTENT
table2
ID
SUBJECT
CONTENT
table3
ID
SUBJECT
CONTENT
... 5 more
我想搜索SUBJECT
所有表格
我怎样才能做到这一点?
table1
ID
SUBJECT
CONTENT
table2
ID
SUBJECT
CONTENT
table3
ID
SUBJECT
CONTENT
... 5 more
我想搜索SUBJECT
所有表格
我怎样才能做到这一点?
CREATE VIEW multitable AS
SELECT * FROM table1
UNION SELECT * from table2
UNION SELECT * from table3;
SELECT subject FROM multitable ...
因为所有表都具有相同的语法,所以您可以使用UNION
运算符。
SELECT * FROM Table1
UNION Table2
UNION Table3
UNION Table4
UNION Table5
UNION Table6
UNION Table7
UNION Table8
WHERE SUBJECT="Subject"
为了简单起见,8个表写出来也不算多。如果你有更多,我会推荐一个动态查询。
select * from table1 where subject like '%match%' union
select * from table2 where subject like '%match%' union
select * from table3 where subject like '%match%' union
select * from table4 where subject like '%match%' union
select * from table5 where subject like '%match%' union
select * from table6 where subject like '%match%' union
select * from table7 where subject like '%match%' union
select * from table8 where subject like '%match%'