mysql返回表名 [英] mysql return table name

查看:319
本文介绍了mysql返回表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个mysql table structure:

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

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

是否可以在这两个表中搜索字符串,然后如果出现该字符串以返回表的名称和行ID?

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 ?

推荐答案

假定两个表的id和news/content具有相同的数据类型,则进行如下查询:

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

这篇关于mysql返回表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆