Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql return table name

Tags:

let's say i have this mysql table structure:

table : articles
----------------
id
content

table : news
------------
id
news

is there a way to search for a string in this two tables and then if the string occurs to return the table's name and the row id ?

like image 291
kmunky Avatar asked Jan 30 '10 13:01

kmunky


2 Answers

Assuming that the two tables have the same datatypes for id and news/content then a query along the lines of

SELECT id, 'articles' as tablename
WHERE content like '%string to search for%'
UNION
SELECT id, 'news' as tablename
WHERE news like '%string to search for%'

Should give you the result you're after

like image 128
Rob Avatar answered Nov 15 '22 07:11

Rob


You could try this:

SELECT 'articles' as table_name, id
FROM `articles` 
WHERE content like '%<my_string>%'

UNION

SELECT 'news' as table_name, id 
FROM `news` 
WHERE news like '%<my_string>%'
like image 34
Dan Soap Avatar answered Nov 15 '22 07:11

Dan Soap