如何获取mysql表列的数据types?
我想获得一个MySQL表的列数据types。
以为我可以使用MYSQLFIELD
结构,但它是枚举字段types。
然后我用mysql_real_query()
我得到的错误是query was empty
我如何获得列数据types?
您可以使用information_schema列表:
SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'tbl_name' AND COLUMN_NAME = 'col_name';
下面的查询返回每个字段的信息列表,包括MySQL字段types。 这里是一个例子:
SHOW FIELDS FROM tablename /* returns "Field", "Type", "Null", "Key", "Default", "Extras" */
请参阅此手册页 。
大多数答案是重复的,将它们分组可能是有用的。 基本上提出了两个简单的选项。
第一个选项
第一个选项有4个不同的别名,其中一些很短:
EXPLAIN db_name.table_name; DESCRIBE db_name.table_name; SHOW FIELDS FROM db_name.table_name; SHOW COLUMNS FROM db_name.table_name;
(注意:作为db_name.table_name
的替代方法,可以使用第二个FROM
: db_name FROM table_name
)。
这给了像这样的东西:
+------------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------+--------------+------+-----+---------+-------+ | product_id | int(11) | NO | PRI | NULL | | | name | varchar(255) | NO | MUL | NULL | | | description | text | NO | | NULL | | | meta_title | varchar(255) | NO | | NULL | | +------------------+--------------+------+-----+---------+-------+
第二个选项
第二个选项稍微长一些:
SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'db_name' AND TABLE_NAME = 'table_name';
这也不那么健谈:
+------------------+-----------+ | column_name | DATA_TYPE | +------------------+-----------+ | product_id | int | | name | varchar | | description | text | | meta_title | varchar | +------------------+-----------+
尽pipe使用AND COLUMN_NAME = 'column_name'
(或like
),但它的优点在于允许按列进行select。
要获取所有列的数据types:
describe table_name
或者只是一个列:
describe table_name column_name
参考这个链接
mysql> SHOW COLUMNS FROM mytable FROM mydb; mysql> SHOW COLUMNS FROM mydb.mytable;
希望这可以帮助你
首先use testDB;
select数据库use testDB;
然后执行
desc `testDB`.`images`; -- or SHOW FIELDS FROM images;
输出:
查询以找出在任何数据库中使用的列的所有数据types
SELECT distinct DATA_TYPE FROM INFORMATION_SCHEMA.columns WHERE table_schema = '<db_name>' AND column_name like '%';
显示列从mytable
自包含完整的例子往往是有用的。
<?php // The server where your database is hosted localhost // The name of your database mydatabase // The user name of the database user databaseuser // The password of the database user thesecretpassword // Most web pages are in utf-8 so should be the database array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") try { $pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "databaseuser", "thesecretpassword", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); } catch(PDOException $e) { die('Could not connect: ' . $e->getMessage()); } $sql = "SHOW COLUMNS FROM mytable"; $query = $pdo->prepare($sql); $query->execute(); $err = $query->errorInfo(); $bug = $err[2]; if ($bug != "") { echo "<p>$bug</p>"; } while ($row = $query->fetch(PDO::FETCH_ASSOC)) { echo "<pre>" . print_r($row, true) . "</pre>"; } /* OUTPUT SAMPLE Array ( [Field] => page_id [Type] => char(40) [Null] => NO [Key] => [Default] => [Extra] => ) Array ( [Field] => last_name [Type] => char(50) More ... */ ?>
ResultSet rs = Sstatement.executeQuery("SELECT * FROM Table Name"); ResultSetMetaData rsMetaData = rs.getMetaData(); int numberOfColumns = rsMetaData.getColumnCount(); System.out.println("resultSet MetaData column Count=" + numberOfColumns); for (int i = 1; i <= numberOfColumns; i++) { System.out.println("column number " + i); System.out.println(rsMetaData.getColumnTypeName(i)); }
请使用下面的mysql查询。
SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM information_schema.columns WHERE table_schema = '<DATABASE NAME>' AND table_name = '<TABLE NAME>' AND COLUMN_NAME = '<COLOMN NAME>'