MySQL多表搜索

时间:2011-10-06 18:22:34

标签: mysql multi-table

table1
 ID
 SUBJECT
 CONTENT

table2
 ID
 SUBJECT
 CONTENT

table3
 ID
 SUBJECT
 CONTENT

... 5 more

我想在所有表格上搜索SUBJECT

我该怎么做?

3 个答案:

答案 0 :(得分:2)

CREATE VIEW multitable AS
SELECT * FROM table1
UNION SELECT * from table2
UNION SELECT * from table3;

SELECT subject FROM multitable ...

答案 1 :(得分:0)

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%'

答案 2 :(得分:0)

因为所有表都具有相同的语法,所以您可以使用UNION运算符。

SELECT * FROM Table1
UNION Table2
UNION Table3
UNION Table4
UNION Table5
UNION Table6
UNION Table7
UNION Table8
WHERE SUBJECT="Subject"

为简单起见,8个表格写出来并不算太多。如果你有更多,我建议dynamic query

相关问题