如何在LIMIT和OFFSET的结果上使用GROUP BY?

时间:2019-04-06 18:04:21

标签: mysql group-by limit offset

我使用MySQL 5.6,使用GROUP BYLIMIT + OFFEST的查询结果有问题。这些结果与没有OFFSET的结果不一致。为什么呢那有什么解决方案?

此查询(无OFFSET和无GROUP BY)给了我10条结果:

SELECT sp.couleur, sp.nom, d.degre_urgence 
FROM demande_interservices AS d 
LEFT JOIN demande_interservices_a_valider AS dv ON d.id = dv.demande_interservices_id  
LEFT JOIN demande_interservices_affectee_a_sous_pole AS d_aff ON d.id = d_aff.demande_interservices_id 
LEFT JOIN sous_pole AS sp ON d_aff.sous_pole_id = sp.id 
WHERE d.pole_sollicite_id = (SELECT pole_id FROM utilisateur WHERE id = 38) 
AND (d.statut_id = 1 OR d.statut_id = 2) 
AND dv.demande_interservices_id IS NULL 
ORDER BY d.degre_urgence DESC 
LIMIT 10

结果:

couleur    degre_urgence    nom
_______________________________

#cd423a    5    Communication
#cd423a    5    Communication
#ff3ebb    5    Assistante DGS
#925210    5    Police Urbanisme
#000000    5    informatique RGPD
#cd423a    5    Communication
#ff3ebb    5    Assistante DGS
#e2c63d    5    Urbanisme - Marchés Publics
#ff3ebb    5    Assistante DGS
#925210    4    Police Urbanisme

GROUP BY相同的查询;

SELECT sp.couleur, sp.nom, d.degre_urgence 
FROM demande_interservices AS d 
LEFT JOIN demande_interservices_a_valider AS dv ON d.id = dv.demande_interservices_id 
LEFT JOIN demande_interservices_affectee_a_sous_pole AS d_aff ON d.id = d_aff.demande_interservices_id 
LEFT JOIN sous_pole AS sp ON d_aff.sous_pole_id = sp.id 
WHERE d.pole_sollicite_id = (SELECT pole_id FROM utilisateur WHERE id = 38) 
AND (d.statut_id = 1 OR d.statut_id = 2) 
AND dv.demande_interservices_id IS NULL 
GROUP BY nom
ORDER BY d.degre_urgence DESC 
LIMIT 10

结果:

couleur    degre_urgence    nom
_______________________________

#e2c63d    5    Urbanisme - Marchés Publics
#000000    5    informatique RGPD
#925210    5    Police Urbanisme
#fff000    2    Accueil    
#7bd026    1    ASVP
#cd423a    1    Communication
#ff3ebb    1    Assistante DGS

但是我期望这些结果:

couleur    degre_urgence    nom
_______________________________

#cd423a    5    Communication
#ff3ebb    5    Assistante DGS
#925210    5    Police Urbanisme
#000000    5    informatique RGPD
#e2c63d    5    Urbanisme - Marchés Publics

2 个答案:

答案 0 :(得分:1)

您必须将第一个查询的结果分组:

select t.nom, min(t.couleur) from (
  SELECT sp.couleur, sp.nom 
  FROM demande_interservices AS d 
  LEFT JOIN demande_interservices_a_valider AS dv ON d.id = dv.demande_interservices_id 
  LEFT JOIN demande_interservices_affectee_a_sous_pole AS d_aff ON d.id = 
  d_aff.demande_interservices_id 
  LEFT JOIN sous_pole AS sp ON d_aff.sous_pole_id = sp.id 
  WHERE d.pole_sollicite_id = (SELECT pole_id FROM utilisateur WHERE id = 38) 
  AND (d.statut_id = 1 OR d.statut_id = 2) 
  AND dv.demande_interservices_id IS NULL 
  ORDER BY d.degre_urgence DESC 
  LIMIT 10
) t
GROUP BY t.nom

MySql允许您使用couleur而不是min(couleur),尽管它不是sql标准。

答案 1 :(得分:0)

您可以使用min()和分组依据

SELECT min(sp.couleur), sp.nom 
FROM demande_interservices AS d 
LEFT JOIN demande_interservices_a_valider AS dv ON d.id = dv.demande_interservices_id  
  AND dv.demande_interservices_id IS NULL 
LEFT JOIN demande_interservices_affectee_a_sous_pole AS d_aff ON d.id = d_aff.demande_interservices_id 
LEFT JOIN sous_pole AS sp ON d_aff.sous_pole_id = sp.id 
WHERE d.pole_sollicite_id = ( SELECT pole_id FROM utilisateur WHERE id = 38) 
AND (d.statut_id = 1 OR d.statut_id = 2) 
GROUP BY sp.nom 
LIMIT 10