[英]Select all columns greater than some value
我是 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 ║
╚═══════════════════╝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.