我想获取mysql表的列数据类型。
以为我可以使用MYSQLFIELD
结构,但它是枚举的字段类型。
然后我尝试了mysql_real_query()
我得到的错误是query was empty
如何获取列数据类型?
答案 0 :(得分:84)
您可以使用information_schema columns表:
SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name' AND COLUMN_NAME = 'col_name';
答案 1 :(得分:61)
下面的查询返回有关每个字段的信息列表,包括MySQL字段类型。这是一个例子:
SHOW FIELDS FROM tablename
/* returns "Field", "Type", "Null", "Key", "Default", "Extras" */
请参阅this manual page。
答案 2 :(得分:25)
大多数答案都是重复的,将它们分组可能很有用。基本上已经提出了两个简单的选项。
第一个选项有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 |
+------------------+-----------+
它的优点是允许每列使用AND COLUMN_NAME = 'column_name'
(或like
)进行选择。
答案 3 :(得分:15)
获取所有列的数据类型:
describe table_name
或只是一列:
describe table_name column_name
答案 4 :(得分:6)
请使用以下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>'
答案 5 :(得分:5)
请参阅此link
mysql> SHOW COLUMNS FROM mytable FROM mydb;
mysql> SHOW COLUMNS FROM mydb.mytable;
希望这可以帮到你
答案 6 :(得分:2)
首先使用use testDB;
选择数据库,然后执行
desc `testDB`.`images`;
-- or
SHOW FIELDS FROM images;
<强>输出:强>
答案 7 :(得分:2)
查询以查找在任何数据库中使用的列的所有数据类型
SELECT distinct DATA_TYPE FROM INFORMATION_SCHEMA.columns
WHERE table_schema = '<db_name>' AND column_name like '%';
答案 8 :(得分:1)
从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 ...
*/
?>
答案 9 :(得分:1)
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));
}
答案 10 :(得分:0)
SELECT *从INFORMATION_SCHEMA.COLUMNS,其中TABLE_SCHEMA ='SCHEMA_NAME'和COLUMN_KEY ='PRI'; WHERE COLUMN_KEY ='PRI';