如何在mysql中使用show columns query时只选择fieldname

时间:2021-06-15 01:25:59

I use this query to select fields in a given table. Is it possible to select only the fieldname and not the whole structure of the table?

我使用此查询来选择给定表中的字段。是否可以只选择字段名而不是表的整个结构?

SHOW COLUMNS FROM student

3 个解决方案

#1


14  

You're trying to determine the table structure? You can query MySQL's information_schema database directly for the fieldnames:

你试图确定表格结构?您可以直接在MySQL的information_schema数据库中查询字段名:

select COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME='student';

#2


0  

The solution mentioned here earlier is not the correct one. Example:

前面提到的解决方案不正确。例:

CREATE DATABASE db1;
CREATE DATABASE db2;
CREATE TABLE db1.t ( id_1 INT);
CREATE TABLE db2.t ( id_2 INT);
SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME  ='t';

This will display:

这将显示:

+-------------+
| COLUMN_NAME |
+-------------+
| id_1        |
| id_2        |
+-------------+

suggesting that the table t has two column which is obviously not true. This query lists all the columns of the tables called t in all of your databases.

建议表t有两列,显然不是真的。此查询列出了所有数据库中名为t的表的所有列。

Instead, you should specify which database contains the table t you want to select the column names from:

相反,您应该指定哪个数据库包含要从中选择列名称的表:

SELECT COLUMN_NAME 
    FROM information_schema.COLUMNS 
    WHERE 
        TABLE_NAME = 't' AND 
        TABLE_SCHEMA = 'db1';

#3


-4  

select COLUMN_NAME FROM TABLE_NAME

选择COLUMN_NAME FROM TABLE_NAME

FOR EXAMPLE: ROLLNO is a column_Name of table Student....

例如:ROLLNO是表Student的column_Name ....

select ROLLNO from Student

从学生中选择ROLLNO

#1


14  

You're trying to determine the table structure? You can query MySQL's information_schema database directly for the fieldnames:

你试图确定表格结构?您可以直接在MySQL的information_schema数据库中查询字段名:

select COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME='student';

#2


0  

The solution mentioned here earlier is not the correct one. Example:

前面提到的解决方案不正确。例:

CREATE DATABASE db1;
CREATE DATABASE db2;
CREATE TABLE db1.t ( id_1 INT);
CREATE TABLE db2.t ( id_2 INT);
SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME  ='t';

This will display:

这将显示:

+-------------+
| COLUMN_NAME |
+-------------+
| id_1        |
| id_2        |
+-------------+

suggesting that the table t has two column which is obviously not true. This query lists all the columns of the tables called t in all of your databases.

建议表t有两列,显然不是真的。此查询列出了所有数据库中名为t的表的所有列。

Instead, you should specify which database contains the table t you want to select the column names from:

相反,您应该指定哪个数据库包含要从中选择列名称的表:

SELECT COLUMN_NAME 
    FROM information_schema.COLUMNS 
    WHERE 
        TABLE_NAME = 't' AND 
        TABLE_SCHEMA = 'db1';

#3


-4  

select COLUMN_NAME FROM TABLE_NAME

选择COLUMN_NAME FROM TABLE_NAME

FOR EXAMPLE: ROLLNO is a column_Name of table Student....

例如:ROLLNO是表Student的column_Name ....

select ROLLNO from Student

从学生中选择ROLLNO