如果在 mySQL 中的表中存在列名/字段名,则搜索数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10101072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:54:50  来源:igfitidea点击:

Search database if column name/field name exists in a table in mySQL

mysqlsqldatabase

提问by rjmcb

Is there a way to search the database if a column name / field name exists in a table in mysql?

如果mysql中的表中存在列名/字段名,有没有办法搜索数据库?

回答by DhruvPathak

use INFORMATION_SCHEMA database and its tables.

使用 INFORMATION_SCHEMA 数据库及其表。

eg :

例如:

    SELECT *
FROM   information_schema.columns
WHERE  table_schema = 'MY_DATABASE'
       AND column_name IN ( 'MY_COLUMN_NAME' );  

回答by Stefan

SHOW COLUMNS FROM tablename LIKE 'columnname'

have fun ! :-)

玩得开心 !:-)

UPDATE:

更新:

As mentioned in the comments, this searches only one table, not the whole database (every table). In that case, please refer to DhruvPathak's answer.

如评论中所述,这仅搜索一张表,而不是整个数据库(每张表)。在这种情况下,请参考 DhruvPathak 的回答。

回答by vikiiii

If you want to search in the whole database then you should try

如果你想在整个数据库中搜索,那么你应该尝试

SELECT * 
FROM information_schema.COLUMNS 
WHERE 
    TABLE_SCHEMA = 'db_name' 
AND COLUMN_NAME = 'column_name'

And if you want to search in the particular table then you should try

如果你想在特定的表中搜索,那么你应该尝试

SELECT * 
FROM information_schema.COLUMNS 
WHERE 
    TABLE_SCHEMA = 'db_name' 
AND TABLE_NAME = 'table_name' 
AND COLUMN_NAME = 'column_name'

回答by Anand

If you want search two or more columns use following below metioned.

如果您想搜索两列或更多列,请使用以下提及的内容。

 SELECT DISTINCT TABLE_NAME 
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE COLUMN_NAME IN ('columnA','ColumnB')
            AND TABLE_SCHEMA='YourDatabase';