[英]How to group by columns twice in MYSQL
I have a database with a table that looks like this with these fields . 我有一个看起来像一个表的数据库这与这些领域 。 I want to return the first_name and surnames of all the pupils and group them by pupil_group, and then by year, and order the groups by alphabetical DESC (pupil_group), ASC (year) and then alphabetical DESC (surname).
我想返回所有学生的名字和姓氏,然后按“瞳孔组”将它们分组,然后按年份分组,并按字母顺序的DESC(pupil_group),ASC(年份)和字母顺序的DESC(姓氏)对组进行排序。
For example, running SELECT * FROM pupils;
例如,运行
SELECT * FROM pupils;
on the table above should return this: 在上表中应返回以下内容:
(((('Jonathan', 'Morris'))), ((('Amber', 'Wilfred))), ((('Grace' ,'Harlow'))), ((('Elizabeth', 'Van Stephen'))), ((('Craig', 'Filip'))), ((('Alice', 'Howard'))), ((('Christopher', 'Paulsen'))), ((('William', 'Chong')), (('Matthew', 'Chong'))), ((('Jessica', 'Harris'))))
So it is in this format: the outer brackets define the collective pupils, the brackets within that define the pupil_groups, the brackets with that define the years, then the brackets within that define the individual pupils. 格式是这样的:外部方括号定义集体学生,内部方括号定义瞳孔组,使用方括号定义年份,然后内部方括号定义单个学生。
Sorry if this is not very clear of if I have made a mistake. 抱歉,如果我做错了,还不是很清楚。 If there is anything I can clarify to make this easier to understand then let me know and I will do so.
如果有什么需要澄清的内容,请让我知道,我会做的。
Currently I have tried SELECT first_name, surname from pupils GROUP BY pupil_group, year ORDER BY pupil_group, year ASC
but it's not merging them into groups or ordering them how I would like. 目前,我已经尝试了
SELECT first_name, surname from pupils GROUP BY pupil_group, year ORDER BY pupil_group, year ASC
但是它并没有将它们合并成组或按照我的SELECT first_name, surname from pupils GROUP BY pupil_group, year ORDER BY pupil_group, year ASC
排序。
Thanks. 谢谢。
EDIT: I've just realised my year
column is a varchar(2)
and so maybe it can't be sorted since it contains numbers. 编辑:我刚刚意识到我的
year
列是varchar(2)
,所以也许无法对其进行排序,因为它包含数字。 I think I'll make that an int
instead. 我想我改成一个
int
。
EDIT2: I updated year
to be int
. EDIT2:我将
year
更新为int
。 I have now tried SELECT first_name, surname, year, pupil_group FROM pupils ORDER BY pupil_group ASC, year ASC, surname ASC
as suggested below (added year
and pupil_group
to make it easier to interpret), but this returns (('Jonathan', 'Morris', 9, 'Apollo'), ('Amber', 'Wilfred', 9, 'Artemis'), ('Grace', 'Harlow', 9, 'Athena'), ('Elizabeth', 'Van Stephen', 9, 'Demeter'), ('Jessica', 'Harris', 10, 'Demeter'), ('Craig', 'Filip', 9, 'Hades'), ('Alice', 'Howard', 9, 'Hera'), ('Christopher', 'Paulsen', 9, 'Hermes'), ('William', 'Chong', 9, 'Poseidon'), ('Matthew', 'Chong', 10, 'Poseidon'))
. 我现在已经尝试按以下建议
SELECT first_name, surname, year, pupil_group FROM pupils ORDER BY pupil_group ASC, year ASC, surname ASC
(添加了year
和pupil_group
以使其更易于理解),但这返回(('Jonathan', 'Morris', 9, 'Apollo'), ('Amber', 'Wilfred', 9, 'Artemis'), ('Grace', 'Harlow', 9, 'Athena'), ('Elizabeth', 'Van Stephen', 9, 'Demeter'), ('Jessica', 'Harris', 10, 'Demeter'), ('Craig', 'Filip', 9, 'Hades'), ('Alice', 'Howard', 9, 'Hera'), ('Christopher', 'Paulsen', 9, 'Hermes'), ('William', 'Chong', 9, 'Poseidon'), ('Matthew', 'Chong', 10, 'Poseidon'))
。 While it does order them correctly, none of them are grouped as I have described. 虽然确实可以正确排序,但它们都没有按照我的描述进行分组。
I think you just are having difficult articulating an ORDER BY
clause: 我认为您只是很难阐明
ORDER BY
子句:
SELECT First_Name, Surname, Pupil_Group, Year
FROM pupils
ORDER BY pupil_group DESC, year, Surname DESC;
This would place all records in the same pupil group together. 这会将所有记录放在同一学生组中。 Then, within each such group, the records would be sorted ascending by the year, followed by descending by the last name.
然后,在每个这样的组中,记录将按年份升序排列,然后按姓氏降序排列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.