我需要查询数据库以获得列名,不要与表中的数据混淆。例如,如果我有一个名为EVENT_LOG的表,其中包含eventID、eventType、eventDesc和eventTime,那么我将希望从查询中检索这些字段名,而不是其他内容。

我发现如何做到这一点:

Microsoft SQL Server MySQL PostgreSQL

但是我需要知道:如何在Oracle中做到这一点?


当前回答

对于SQLite,我相信你可以使用如下内容:

PRAGMA table_info(table-name);

来自sqlite.org的解释:

这个pragma为命名表中的每一列返回一行。结果集中的列包括列名、数据类型、列是否可以为NULL以及列的默认值。对于不属于主键的列,结果集中的“pk”列为零;对于属于主键的列,结果集中的“pk”列是主键中的列的索引。

参见:Sqlite.org Pragma表信息

其他回答

select column_name,* from information_schema.columns
 where table_name = 'YourTableName'
order by ordinal_position

对于SQL Server 2008,我们可以使用information_schema。列用于获取列信息

SELECT *
FROM   information_schema.columns
WHERE  table_name = 'Table_Name'
ORDER  BY ordinal_position  
SELECT A.COLUMN_NAME, A.* FROM all_tab_columns a 
WHERE table_name = 'Your Table Name'
AND A.COLUMN_NAME = 'COLUMN NAME' AND a.owner = 'Schema'

对于SQL Server:

SELECT [name] AS [Column Name]
FROM syscolumns
WHERE id = object_id('TABLE_NAME')

答案在这里:http://php.net/manual/en/function.mysql-list-fields.php 在你的情况下,我会使用以下代码:

$result = mysql_query("SHOW COLUMNS FROM sometable");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$fields = array();
if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
        $fields[] = $row['Field'];
    }
}