简体   繁体   English

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

[英]How to select only fieldname when using show columns query in mysql

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

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';

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. 建议表t有两列,显然不是真的。 This query lists all the columns of the tables called t in all of your databases. 此查询列出所谓的表的所有列t所有的数据库。

Instead, you should specify which database contains the table t you want to select the column names from: 相反,你应该指定数据库包含表t要选择从列名:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM