mysql讯搜获取所有数据_MySQL查询以搜索完整数据库中的记录

bd96500e110b49cbb3cd949968f18be7.png

i'm searching for a SQL query to run through PhpMyAdmin to find all records in all the tables of the database that contains a given string.

till now all i've found is a query:

SELECT * FROM information_schema.`COLUMNS` C WHERE TABLE_SCHEMA = 'final_year_project_demo'

that returns all the tables in the schema , but i'm not efficient in SQL so not able to move any further than that.

all i need is a query that will list all the records(containing a particular string in any column) from all the tables that are listed through above code.

columns in all tables are same.

解决方案

This is a somewhat complicated thing to do. And you really can't do it in one step. I'll give you something to start with, and you'll have to take it from there:

select CONCAT("SELECT * FROM ", TABLE_NAME, " WHERE user_id=1;") FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME="user_id";

Now this will produce output like this:

+--------------------------------------------------------------------+

| CONCAT("SELECT * FROM ", TABLE_NAME, " WHERE user_id=1;") |

+--------------------------------------------------------------------+

| SELECT * FROM table0 WHERE user_id=1; |

| SELECT * FROM table1 WHERE user_id=1; |

Now, you want to turn around and execute all of those commands... so do it like this:

select CONCAT("SELECT * FROM ", TABLE_NAME, " WHERE user_id=1;") FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME="user_id" INTO OUTFILE "some_file_path_and_name";

That will give you a text file full of all of the commands you are looking for.

Update---

I missed the "For any column bit.."

select CONCAT("SELECT * FROM ", TABLE_NAME, " WHERE ", COLUMN_NAME, "='WHATEVER';") FROM INFORMATION_SCHEMA.COLUMNS WHERE COLLATION_NAME IS NOT NULL INTO OUTFILE 'somepath';

Here, we are using the fact that you said you are looking for a string, and all of the string type fields have a collation_name. Replace WHATEVER with what you are looking for.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值