SQL:我每个用户只返回一行

时间:2017-03-27 09:02:40

标签: sql

我需要为每个用户返回游戏“抽认卡”的所有行。

目前我只为每个用户返回一行。

SELECT  
    s.name as Escuela,
    sc.class_description as Clase, 
    u.level as "Nivel Del Alumno",
    CONCAT(u.firstname," ",u.lastname) as Nombre,
    stats.game_type as "Tipo de Juego",
    MIN(stats.time) as "Tiempo en Segundos",
    SEC_TO_TIME(stats.time) as "Tiempo en Formato"
FROM 
    game_statistics stats 
INNER JOIN 
    users1 u ON stats.user_id = u.id
INNER JOIN 
    school_classes sc ON u.class_id = sc.id
INNER JOIN 
    schools s ON s.id = sc.school_id
WHERE 
    stats.game_type LIKE 'flashcards' 
GROUP BY 
    s.name, sc.class_description,
    u.level, Nombre, stats.game_type
ORDER BY 
    s.name, sc.class_description,
    u.level, stats.game_type, stats.time

1 个答案:

答案 0 :(得分:0)

使用以下查询。您必须包含%的外卡字符才能获取包含文本flashcard的所有行。每当您在查询中使用LIKE运算符时,您应该使用通配符,否则使用LIKE的目的就会失败。

SELECT
    s.name AS Escuela
   ,sc.class_description AS Clase
   ,u.level AS "Nivel Del Alumno"
   ,CONCAT(u.firstname, " ", u.lastname) AS Nombre
   ,stats.game_type AS "Tipo de Juego"
   ,MIN(stats.TIME) AS "Tiempo en Segundos"
   ,SEC_TO_TIME(stats.TIME) AS "Tiempo en Formato"
  FROM game_statistics stats
  INNER JOIN users1 u
    ON stats.user_id = u.id
  INNER JOIN school_classes sc
    ON u.class_id = sc.id
  INNER JOIN schools s
    ON s.id = sc.school_id
  WHERE stats.game_type LIKE '%flashcards%'
  GROUP BY s.name
          ,sc.class_description
          ,u.level
          ,Nombre
          ,stats.game_type
  ORDER BY s.name, sc.class_description, u.level, stats.game_type, stats.TIME