在 MySQL 中從 SHOW COLUMNS 中排除特定列?


我們首先建立一個示例表

mysql> create table excludeCertainColumnsDemo
   -> (
   -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentName varchar(100),
   -> StudentAge int,
   -> StudentMarks int,
   -> StudentAddress varchar(200)
   -> );
Query OK, 0 rows affected (0.50 sec)

現在你可以藉助 desc 命令來檢查表格說明。查詢如下所示 −

mysql> desc excludeCertainColumnsDemo;

輸出如下

+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| StudentId      | int(11)      | NO   | PRI | NULL    | auto_increment |
| StudentName    | varchar(100) | YES  |     | NULL    |                |
| StudentAge     | int(11)      | YES  |     | NULL    |                |
| StudentMarks   | int(11)      | YES  |     | NULL    |                |
| StudentAddress | varchar(200) | YES  |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

這裡有一個查詢,用於從 SHOW COLUMNS 中排除某些列。你需要排除 'StudentAge' 和 'StudentMarks' 列。查詢如下所示 −

mysql> SHOW COLUMNS FROM excludeCertainColumnsDemo WHERE Field NOT IN ('StudentAge', 'StudentMarks');

輸出如下

+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| StudentId      | int(11)      | NO   | PRI | NULL    | auto_increment |
| StudentName    | varchar(100) | YES  |     | NULL    |                |
| StudentAddress | varchar(200) | YES  |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

更新於:30-Jul-2019

267 次瀏覽

開啟您的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.