table1
ID
SUBJECT
CONTENT
table2
ID
SUBJECT
CONTENT
table3
ID
SUBJECT
CONTENT
... 5 more
我想對所有表MySQL的多功能表搜索
搜索SUBJECT
我怎樣才能做到這一點?
table1
ID
SUBJECT
CONTENT
table2
ID
SUBJECT
CONTENT
table3
ID
SUBJECT
CONTENT
... 5 more
我想對所有表MySQL的多功能表搜索
搜索SUBJECT
我怎樣才能做到這一點?
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%'
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個表格並沒有太多要寫出來。如果你有更多,我會推薦dynamic query。
這些表是否都是相同的結構?如果是這樣,爲什麼他們不被標準化爲一張桌子?如果不是,你打算如何組合結果?您是在進行全文搜索還是僅僅進行常規搜索? – mellamokb
與表格相同的結構但不同的內容。 – Awersione