我有这个SQL查询
查询
Select * from `trees` WHERE id = '1' AND ( `apple`='1' OR `banana` = '1' OR `coconut` = '1' OR `pear` ='1') ;
这是我的桌子可以说
Tree_table
id | apple | banana | coconut | pear|
1 1 1 1 0
2 0 0 1 1
3 1 0 0 1
所以我希望我的素养
apple
banana
coconut
使用SQL查询甚至PHP都可以做到
答案 0 :(得分:1)
您需要取消透视数据。在MySQL中,用union all
最容易做到:
select tree
from ((select id, 'apple' as tree
from trees
where apple = 1
) union all
(select id, 'banana' as tree
from trees
where banana = 1
) union all
(select id, 'coconut' as tree
from trees
where coconut = 1
) union all
(select id, 'pear' as tree
from trees
where pear = 1
)
) t
where id = 1;