背景
有一个商品的名称配置错误了,需要进行修改,但是涉及到的表太多了,因为商品的sku_name被冗余到了很多表中,一个一个的找非常的费事费力,特地记下便捷查询操作以备后用。
数据库SQL快捷查询
1.查询包含某个字段的所有表名
代码语言:javascript复制SELECT
DISTINCT table_name
FROM information_schema.columns
WHERE
table_schema = 'db_lingyejun' and column_name='sku_id';
2.查询同时含有两个字段的所有表名
代码语言:javascript复制SELECT
DISTINCT a.table_name
FROM information_schema.columns a,
information_schema.columns b
WHERE
a.table_name = b.table_name
and a.table_schema = 'db_lingyejun' and a.column_name='sku_id'
and b.table_schema = 'db_lingyejun' and b.column_name='sku_name';
3.拼接SQL动态生成针对此字段的所有更新语句
代码语言:javascript复制SELECT
CONCAT('UPDATE ', a.table_name, 'set sku_name = "IPHONE 13PRO" where sku_id = 1106;')
FROM information_schema.columns a,
information_schema.columns b
WHERE
a.table_name = b.table_name
and a.table_schema = 'db_lingyejun' and a.column_name='sku_id'
and b.table_schema = 'db_lingyejun' and b.column_name='sku_name';
首发链接:https://cloud.tencent.com/developer/article/2285762