0

我想从没有特定值的 mysql 表中获取列名

MyTable:
------------------------------------------
| col1  |  col2  |  col3 | col4  | col5  |
|       |        |       |       |       |
|  12   |   -    |   50  |   -   |  10   |
|       |        |       |       |       |
------------------------------------------

我想要没有值'-'的列名

Output :
-----------------
|  col1  |  12  |
|  col3  |  15  |
|  col5  |  10  |
-----------------

有这个查询吗?

想要像这样的输出:

    SELECT coloumn_names FROM table_name WHERE column_value != '-' AND other_col_value='some_value'

其中 some_col_value 是唯一字段

4

3 回答 3

0

Do not mix code with data.
the moment you want to select a column name (which is a part of the program) as a mere data - you know you're doing something wrong.

You need another table structure for this.
It have to be vertical, not horizontal.

Just make it

id | key  | value  | 
 1   col1    12
 1   col2     -
 1   col3    50
 1   col4     -
 1   col5    10

where id - identifier from your old table

select key from old join new on id where value='-' and col6='whatever'
于 2013-10-29T08:03:13.587 回答
-1
SELECT "col1" colName, col1 colValue
FROM MyTable
WHERE col1 != "-" AND col6 = some_number
UNION
SELECT "col2", col2
FROM MyTable
WHERE col2 != "-" AND col6 = some_number
UNION
SELECT "col3", col3
FROM MyTable
WHERE col3 != "-" AND col6 = some_number
UNION
...
于 2013-10-29T07:54:27.367 回答
-1
 SHOW COLUMNS FROM `tablename` where 'condition'

有关更多详细信息,请参阅http://dev.mysql.com/doc/refman/5.0/en/show-columns.html 希望对您有所帮助

于 2013-10-29T09:12:54.770 回答