Search code examples
phpmysqlsearchwildcardsql-like

How to search using the LIKE syntax and % WILDCARD % across multiple tables


How can you use mysql and the like/wildcard syntax across multiple tables, would it be as simple as:

(SELECT * FROM `table1` WHERE `name` LIKE '%tom%') AND (SELECT * FROM `table2` WHERE `name` LIKE '%sam%')

Not tested, just thinking about it.


Solution

  • If your tables have the same structures, you can use UNION:

    SELECT * FROM `table1` WHERE `name` LIKE '%tom%' UNION SELECT * FROM `table2` WHERE `name` LIKE '%sam%'