让我说我有mysql table structure
:
table : articles
----------------
id
content
table : news
------------
id
news
有没有办法在这两个表中搜索字符串,然后是否出现字符串以返回表的名称和行ID?
答案 0 :(得分:3)
假设这两个表具有相同的id和news / content数据类型,那么就是
的查询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%'
应该给你你之后的结果
答案 1 :(得分:1)
你可以试试这个:
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>%'
答案 2 :(得分:1)
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'