更新时间:2022-12-10 09:27:53
如果添加
SET SESSION sql_mode = 'ONLY_FULL_GROUP_BY';
然后,您将看到为什么其他RDBMS不允许使用此语法的原因:
Then you'll see the why other RDBMS won't allow this syntax:
如果要分组而不是DISTINCT,则需要对SELECT中的所有列进行分组
If you want to GROUP BY rather then DISTINCT, then you need to GROUP BY all column in the SELECT
SELECT s.serviceid, s.servicenameit, s.servicenameen
FROM services s, kpi k, checklist_has_kpi chk
WHERE s.serviceid=k.serviceid AND k.kpiid=chk.kpiid AND k.inreport='yes' AND chk.checklistid=61
GROUP BY s.serviceid, s.servicenameit, s.servicenameen
但是,无论使用GROUP BY还是DISTINCT,您都没有chk.order
可以进行排序
But then you have no chk.order
to order by, whether using GROUP BY or DISTINCT
那怎么办,完全忽略重复项?
So what about this, ignoring duplicates completely?
SELECT s.serviceid, s.servicenameit, s.servicenameen
FROM services s, kpi k, checklist_has_kpi chk
WHERE s.serviceid=k.serviceid AND k.kpiid=chk.kpiid AND k.inreport='yes' AND chk.checklistid=61
ORDER BY chk.order ASC
或者将此命令按3x services
列的最早顺序排序
Or this to ORDER BY the earliest order per 3x services
columns
SELECT s.serviceid, s.servicenameit, s.servicenameen
FROM services s, kpi k, checklist_has_kpi chk
WHERE s.serviceid=k.serviceid AND k.kpiid=chk.kpiid AND k.inreport='yes' AND chk.checklistid=61
GROUP BY s.serviceid, s.servicenameit, s.servicenameen
ORDER BY MIN(chk.order)