Question

I have three tables like:

  • tbl_1 -> {'id'-'name'-'price'}
  • tbl_2 -> {'id'-'name'-'price'}
  • tbl_3 -> {'id'-'name'-'price'}

I want to search for Keyword in name field of all the three tables. How can I do it?

Was it helpful?

Solution

Why having three tables with the same content? Just create one table and add another field 'description' or 'description_id'.

This should work...

SELECT id, name, price FROM tbl_1 WHERE name LIKE '%Keyword%'
UNION
SELECT id, name, price FROM tbl_2 WHERE name LIKE '%Keyword%'
UNION
SELECT id, name, price FROM tbl_3 WHERE name LIKE '%Keyword%'

Keep in mind that this will find any string in that field, if you'd like to search for exact string remove the '%'.

OTHER TIPS

Use UNION to search in multiple tables and return a combined result:

SELECT `id`, `name`, `price`
FROM `tbl_1` WHERE `name` = 'keyword'
UNION
SELECT `id`, `name`, `price`
FROM `tbl_2` WHERE `name` = 'keyword'
UNION
SELECT `id`, `name`, `price`
FROM `tbl_3` WHERE `name` = 'keyword' 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top