sql >> Base de Datos >  >> RDS >> Mysql

Seleccione todas las columnas mayores que algún valor

Su estructura de datos no está normalizada. Pero si quieres ir por este camino usa:

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

SqlFiddleDemo

Salida:

╔══════════╗
║ student  ║
╠══════════╣
║ Student1 ║
║ Student2 ║
╚══════════╝

Si desea usar resultados separados por comas:

select GROUP_CONCAT(sub.student ORDER BY sub.student) AS result

SqlFiddleDemo2

Salida:

╔═══════════════════╗
║       result      ║
╠═══════════════════╣
║ Student1,Student2 ║
╚═══════════════════╝