Answers:
您可以使用information_schema列表:
SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name' AND COLUMN_NAME = 'col_name';
SELECT COLUMN_TYPE
用来获取该额外信息。
AND COLUMN_NAME = 'col_name'
),则除非显示列名:,否则此查询无用SELECT COLUMN_NAME, DATA_TYPE FROM etc.
。
下面的查询返回有关每个字段的信息列表,包括MySQL字段类型。这是一个例子:
SHOW FIELDS FROM tablename
/* returns "Field", "Type", "Null", "Key", "Default", "Extras" */
请参阅本手册页。
WHERE FIELD = '<your column name>'
如果你想要一个列信息。
大多数答案都是重复的,将它们分组可能会很有用。基本上已经提出了两个简单的选择。
第一个选项有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
)按列选择。
要获取所有列的数据类型:
describe table_name
或仅是一列:
describe table_name column_name
请使用下面的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>'
SELECT DATA_TYPE
像其他答案所建议的那样使用更好吗?
首先使用选择数据库,use testDB;
然后执行
desc `testDB`.`images`;
-- or
SHOW FIELDS FROM images;
输出:
查询以找出任何数据库中正在使用的列的所有数据类型
SELECT distinct DATA_TYPE FROM INFORMATION_SCHEMA.columns
WHERE table_schema = '<db_name>' AND column_name like '%';
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 ...
*/
?>
php
标签
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));
}
java
。
SELECT * FROM INFORMATION_SCHEMA.COLUMNS,其中TABLE_SCHEMA ='SCHEMA_NAME'和COLUMN_KEY ='PRI'; WHERE COLUMN_KEY ='PRI';