Pregunta

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?

¿Fue útil?

Solución

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 '%'.

Otros consejos

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' 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top