查询以检索列名称

时间:2015-01-04 19:38:30

标签: mysql sql mysqli phpmyadmin

表格结构:

enter image description here

情景:

要检索列的名称,如果它们不为空,那么对于Patch_No = 0.02,Champion = Katarina

这是我提出的查询,但遗憾的是它无法正常工作 编辑下面的查询给了我Passive,Q,W,E,R所以看起来好像有问题(被动不是空或[...])

查询:

 SELECT column_name 
from information_schema.columns, patches.champions
 where 
table_name = 'champions'
AND (column_name ='Passive' OR column_name='Q' OR column_name ='W' OR column_name='E' or            column_name='R')
AND (Passive is not null or Q is not null or W is not null or E is not null or R is not null)
AND Patch_No = '0.02'
AND Champion = 'Katarina'

我试图只为这个补丁和冠军测试Passive,它总是给我5个被动,就像它没有考虑Patch_No和Champion限制一样

EDIT2

select isc.COLUMN_NAME, c.* 
from INFORMATION_SCHEMA.COLUMNS isc, patches.champions c
where TABLE_NAME = 'champions' 
AND Patch_No = 0.02
AND Champion = 'Katarina'

and (
    (isc.COLUMN_NAME = 'Passive' and c.Passive is not null)
    or (isc.COLUMN_NAME = 'Q' and c.Q is not null)
    or (isc.COLUMN_NAME = 'W' and c.W is not null)
    or (isc.COLUMN_NAME = 'E' and c.E is not null)
    or (isc.COLUMN_NAME = 'R' and c.R is not null)
)
order by
     c.ID

2 个答案:

答案 0 :(得分:1)

试试这个。

注意,在where子句中,我假设patch_no是一个数值数据类型,已经从patch_no的比较中删除了引号。如果它实际上是一个字符串,你需要将它们放回去。

select isc.COLUMN_NAME, c.* 
from INFORMATION_SCHEMA.COLUMNS isc, patches.champions c
where TABLE_NAME = 'SO_27769554' 
AND Patch_No = .02
AND Champion = 'Katrina'

and (
        (isc.COLUMN_NAME = 'pasive' and c.pasive is not null)
        or (isc.COLUMN_NAME = 'q' and c.q is not null)
        or (isc.COLUMN_NAME = 'w' and c.w is not null)
        or (isc.COLUMN_NAME = 'e' and c.e is not null)
        or (isc.COLUMN_NAME = 'r' and c.r is not null)
    )
order by
    c.id

答案 1 :(得分:0)

所以这实际上对我有用,我主要从G B获取代码,感谢您的帮助!

SELECT column_name 
from information_schema.columns, patches.champions
where 
table_name = 'champions'
AND ((column_name ='Passive' and Passive!='')
or (column_name='Q' and Q!='')
or (column_name='W' and W!='')
or (column_name='E' and E!='')
or (column_name='R' and R!='')

)

 AND Patch_No = '0.02'
AND Champion = 'Katarina'