如何从表中获取选定的列名称

时间:2015-11-06 17:00:35

标签: mysql

如何从表中获取选定的列名?

在我的表中,我总共有10行,但我想只显示5行。这就是我选择的列的意思。

对上述情况有什么疑问?

我使用下面的Query来检索所有列:

SELECT column_name
FROM information_schema.columns
WHERE table_name = 'table name'

3 个答案:

答案 0 :(得分:1)

Mysql Limit

SELECT column_name FROM information_schema.columns WHERE table_name = 'table name'  limit 5

并限制随机

SELECT column_name FROM information_schema.columns WHERE table_name = 'table name' 
ORDER BY RAND()
LIMIT 5

答案 1 :(得分:0)

您必须具有检索行的条件:

SELECT coloumn from table_name WHERE condition

注意:条件的例子可以像" no_of_employees = 2"

上面的例子将检索coloumn" no_of_employees"是2

要获得随机行,请参阅以下帖子:http://www.petefreitag.com/item/466.cfm

使用MySQL选择一个随机行:

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

使用PostgreSQL选择一个随机行:

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

使用Microsoft SQL Server选择随机行:

SELECT TOP 1 column FROM table
ORDER BY NEWID()
Select a random row with IBM DB2
SELECT column, RAND() as IDX 
FROM table 
ORDER BY IDX FETCH FIRST 1 ROWS ONLY

使用Oracle选择随机记录:

SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

答案 2 :(得分:0)

最后我找到了查询...即,

SELECT DISTINCT column_name 
FROM information_schema.columns  
WHERE table_name IN (SELECT table_name FROM information_schema.tables
                     WHERE table_schema='your database') 
  AND column_name IN (SELECT column_name FROM information_schema.columns
                      WHERE column_name IN ('column1','column2','etc')
                        AND table_name IN ('your table name'))