MySQL表有多少列

本文关键字:多少 MySQL | 更新日期: 2023-09-27 18:14:52

我有一个表:

SHOW COLUMNS FROM `darbuotojai`;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(10)      | NO   | PRI | NULL    | auto_increment |
| vardas       | char(20)     | YES  |     | NULL    |                |
| pavarde      | char(30)     | YES  |     | NULL    |                |
| email        | char(100)    | YES  |     | NULL    |                |
| pareigos     | char(50)     | YES  |     | NULL    |                |
| ilg_tel_nr   | decimal(8,0) | NO   | PRI | 0       |                |
| trump_tel_nr | decimal(3,0) | NO   | PRI | 0       |                |
| inv_nr       | char(10)     | YES  |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

如何使用sql命令对表中的列进行计数?我如何识别他们?

MySQL表有多少列

SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'darbuotojai'
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'database_name'
  AND table_name = 'tbl_name'
SELECT count(*) FROM information_schema.`COLUMNS` C
WHERE table_name = 'your_table_name'
AND TABLE_SCHEMA = "your_db_name"

只有当表名存在于多个数据库中时,才需要TABLE_SCHEMA