MySql : SELECT COLUMNS WHERE COLUMNS VALUE ARE SAM

2019-09-20 04:06发布

问题:

This is my table data..

column_1_____column_3_____column_4_____column_5_____column_6_____column_7_____column_8 
   yes         no           yes           yes          yes          no           yes   

here, their is only one datarow

i want only that columns which has value = 'yes'.

for this which query works? Thanks.

回答1:

SQL is not organized around columns. It is organized around rows. You can do what you want with a query like this:

select 'column1' as col
from t
where column1 = 'yes'
union all
select 'column2' as col
from t
where column2 = 'yes'
union all
. . .
union all
select 'column8' as col
from t
where column8 = 'yes';


回答2:

Your idea isn't suitable for sql logic. Because you want to know something, before query hasn't work yet.



标签: mysql sql mysqli