Mysql 基于查询获取表的列名
我有一个SQL查询 查询Mysql 基于查询获取表的列名,mysql,sql,Mysql,Sql,我有一个SQL查询 查询 Select * from `trees` WHERE id = '1' AND ( `apple`='1' OR `banana` = '1' OR `coconut` = '1' OR `pear` ='1') ; 这是我的桌子 树_表 id | apple | banana | coconut | pear| 1 1 1 1 0 2 0 0 1 1 3
Select * from `trees` WHERE id = '1' AND ( `apple`='1' OR `banana` = '1' OR `coconut` = '1' OR `pear` ='1') ;
这是我的桌子
树_表
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也可以执行此操作?您需要取消对数据的绑定。在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;
@o3203823。如果你特别需要PHP代码,你应该问另一个问题,并把重点放在问题的PHP方面。不,这很好,但我只是想知道如何让PHP来执行这个问题