views:

141

answers:

4

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.

+2  A: 

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%'
Ben
A: 

Use UNION

(SELECT * FROM `table1` WHERE `name` LIKE '%tom%') UNION
 (SELECT * FROM `table2` WHERE `name` LIKE '%sam%')
Dani Cricco
A: 

Actually if you use UNION, any duplicate rows may be removed. Use UNION ALL if you wish to retain any duplicates (say if you want to perform a COUNT on all rows matching %tom% in table1 and table2.

(SELECT * FROM `table1` WHERE `name` LIKE '%tom%') UNION ALL (SELECT * FROM `table2` WHERE `name` LIKE '%tom%')
eckesicle
A: 

dont post something if its not tested yet! it may mislead the readers

erick