如果你的表是這樣的:
+-----+-------+-------+-------+-----+-------+
| id | col_1 | col_2 | col_3 | ... | col_N |
+-----+-------+-------+-------+-----+-------+
| 1 | true | NULL | false | ... | true |
| 2 | NULL | NULL | true | ... | true |
| 3 | false | false | true | ... | true |
| ... | ... | ... | ... | ... | ... |
| M | true | true | NULL | ... | false |
+-----+-------+-------+-------+-----+-------+
則:
select (
concat(
if(col_1, 'col_1, ', ''),
if(col_2, 'col_2, ', ''),
if(col_3, 'col_3, ', ''),
...
if(col_N, 'col_N, ', '')
)
) as `list_of_cols`
from
`table_name`
where `id` = id_value;
結果會是這樣的:
+----------------------+
| list_of_cols |
+----------------------+
| col_1, col_5, col_6, |
+----------------------+
這不能工作原樣。您無法在一個表中針對另一個表中某個其他字段的VALUE值加入字段。如果您已經知道布爾字段的名稱,那麼您爲什麼首先查詢information_schema? – 2015-02-09 19:28:05
感謝您的回覆Marc。它會動態地爲每個用戶改變這個布爾值的變化。 – 2015-02-09 19:30:19
是嗎?這是你桌子上的一個領域。 'select * from yourtable where booleanfield = true'將得到你所有記錄的那個字段爲真。 – 2015-02-09 19:31:17