could anyone tell me how to select particular column if i dont know the schema of table .Like if i want only the values in the second column of database table without knowing the column name. Is there a way to do that?
Not sure what you wanted to do. And I sincerely expect you don't do what you're doing. But if you jsut wanted to know how to play with technicality. Here is an example to do it. There may be better ways.
mysql> desc test;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(100) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from test;
+------+-------+
| id | name |
+------+-------+
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
| 4 | name4 |
+------+-------+
4 rows in set (0.00 sec)
mysql> select @colid:=column_name from information_schema.columns where table_schema='test' and table_name='test' and ordinal_position=2;
+---------------------+
| @colid:=column_name |
+---------------------+
| name |
+---------------------+
1 row in set (0.01 sec)
mysql> set @sqlstr:=concat('select ', @colid, ' from test');
Query OK, 0 rows affected (0.00 sec)
mysql> prepare sttmt from @sqlstr;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> execute sttmt;
+-------+
| name |
+-------+
| name1 |
| name2 |
| name3 |
| name4 |
+-------+
4 rows in set (0.00 sec)
refer the last example here http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
By the way, Google gives first link as perfect solution. Select statement with column number instead of column names