[英]How to search using the LIKE syntax and % WILDCARD % across multiple tables
您如何在多個表中使用mysql和like / wildcard語法,會如此簡單:
(SELECT * FROM `table1` WHERE `name` LIKE '%tom%') AND (SELECT * FROM `table2` WHERE `name` LIKE '%sam%')
沒有測試,只是考慮一下。
如果表具有相同的結構,則可以使用UNION :
SELECT * FROM `table1` WHERE `name` LIKE '%tom%' UNION SELECT * FROM `table2` WHERE `name` LIKE '%sam%'
使用UNION
(SELECT * FROM `table1` WHERE `name` LIKE '%tom%') UNION
(SELECT * FROM `table2` WHERE `name` LIKE '%sam%')
實際上,如果您使用UNION,則可能會刪除任何重復的行。 如果要保留任何重復項,請使用UNION ALL(例如,如果要對與table1和table2中的%tom%匹配的所有行執行COUNT)。
(SELECT * FROM `table1` WHERE `name` LIKE '%tom%') UNION ALL (SELECT * FROM `table2` WHERE `name` LIKE '%tom%')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.