mysql中的列名

时间:2013-10-29 07:50:46

标签: mysql mysqli

我想从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是唯一字段

3 个答案:

答案 0 :(得分:0)

不要将代码与数据混合 你想要选择一个列名(这是程序的一部分)作为一个单纯的数据 - 你知道你做错了什么。

你需要另一个表结构。
它必须是垂直的,而不是水平的。

只是做到了

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

其中id - 旧表中的标识符

select key from old join new on id where value='-' and col6='whatever'

答案 1 :(得分:-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
...

答案 2 :(得分:-1)

 SHOW COLUMNS FROM `tablename` where 'condition'

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