mysql return table name

一曲冷凌霜 提交于 2019-12-05 11:12:06

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

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>%'
SELECT * FROM (

SELECT id, content as text, 'articles' as tablename
FROM articles

UNION ALL

SELECT  id, news as text, 'news' as tablename
FROM news

) as tmp 

WHERE text = 'SEARCH_TERM'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!