我是mysql的新手,我具有这样的结构
timestamp | Name | Total | Student1 | Student2 | Student3
20150911 | X | 100 | 76 | 10 | NA
20151111 | Y | 70 | NA | 0 | 23
现在我想用mysql查询选择给定的“时间戳”在哪里
预期结果
我已经尝试过搜索这种混合选择,但到目前为止,我还没有成功。大多数答案都集中在行选择而不是列上。
您的数据结构未规范化。但是,如果您想走这条路,请使用:
select sub.student
FROM (
select t.timestamp,
t.Name,
t.Total,
c.col AS student,
case c.col
when 'Student1' then Student1
when 'Student2' then Student2
when 'Student3' then Student3
-- ...
end as d
from mytable t
cross join
(
select 'Student1' as col
union all select 'Student2'
union all select 'Student3'
-- ...
) c
) AS sub
WHERE sub.timestamp = '20150911'
AND sub.d > 0;
-- sub.d = 'NA'
-- sub.d = 0
输出:
╔══════════╗
║ student ║
╠══════════╣
║ Student1 ║
║ Student2 ║
╚══════════╝
如果要用逗号分隔结果,请使用:
select GROUP_CONCAT(sub.student ORDER BY sub.student) AS result
输出:
╔═══════════════════╗
║ result ║
╠═══════════════════╣
║ Student1,Student2 ║
╚═══════════════════╝
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句