0

How can I get selected columns name from a table?

In my table I have a total of 10 rows, but I want to display only 5 rows. That is what I mean by selected columns.

What would be a query for the above case?

I am using the below Query to retrieve all columns:

SELECT column_name
FROM information_schema.columns
WHERE table_name = 'table name'
whereisSQL
  • 638
  • 2
  • 13
  • 23
  • 2
    Possible duplicate of [Get table column names in mysql?](http://stackoverflow.com/questions/1526688/get-table-column-names-in-mysql) – approxiblue Nov 07 '15 at 03:22

3 Answers3

1

with Mysql Limit

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

And for limit random

SELECT column_name FROM information_schema.columns WHERE table_name = 'table name' 
ORDER BY RAND()
LIMIT 5
ashkufaraz
  • 5,179
  • 6
  • 51
  • 82
  • dude in my table i have lot of cloumns form that i want to chosse particular rows for example i have employee table. in that i have Empno,empname,sal,designation,address columns....now i want to display empname,sal,address column ..so how can i write the query? – user5530684 Nov 06 '15 at 17:18
0

You must have a condition to retrieve rows:

SELECT coloumn from table_name WHERE condition

Note: example for condition can be like " no_of_employees=2"

The above example will retrieve rows in which the coloumn "no_of_employees" is 2

For getting a random row, refer this post: http://www.petefreitag.com/item/466.cfm

Select a random row with MySQL:

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

Select a random row with PostgreSQL:

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

Select a random row with 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

Select a random record with Oracle:

SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1
  • dude in my table i have lot of cloumns form that i want to chosse particular rows for example i have employee table. in that i have Empno,empname,sal,designation,address columns....now i want to display empname,sal,address column ..so how can i write the query? – user5530684 Nov 06 '15 at 17:20
  • This may help: http://stackoverflow.com/questions/5420985/how-to-select-some-particular-columns-from-a-table-if-the-table-has-more-than-10 – Aashish Gangwani Nov 06 '15 at 17:36
0

Finally I Find the query ...i.e.,

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'))
sshine
  • 15,635
  • 1
  • 41
  • 66