假设我有这个MySQL表结构
:
table : articles
----------------
id
content
table : news
------------
id
news
有没有一种方法可以在这两个表中搜索字符串,然后如果字符串出现,则返回表的名称和行ID?
假设我有这个MySQL表结构
:
table : articles
----------------
id
content
table : news
------------
id
news
有没有一种方法可以在这两个表中搜索字符串,然后如果字符串出现,则返回表的名称和行ID?
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%'
这应该可以给您所需的结果。
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'