比方說,我有這個mysql table structure
:MySQL的返回表名
table : articles
----------------
id
content
table : news
------------
id
news
有沒有辦法來搜索這兩個表一個字符串,然後如果發生字符串返回表的名稱和行號?
比方說,我有這個mysql table structure
: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>%'
哼哼......想知道爲什麼我的回答得到了downvote?評論會有幫助! – 2010-01-30 19:17:14
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'
我只是猜測這裏,但是從表名我想,他要搜索的字符串作爲子串,而不是完全匹配。 – 2010-01-30 14:06:53
yup,你是對的 – kmunky 2010-01-30 14:16:24
@kmunky,改變它以符合你的確切要求:) – Rob 2010-01-30 14:32:09