我真的试图解决这一难题: https://www.hackerrank.com/challenges/the-padsSQL - UNION不下令第二选择
我的解决办法是这样的在MySQL:
(SELECT CONCAT(Name,'(',SUBSTR(Occupation,1,1),')') FROM Occupations ORDER BY Name)
UNION
(SELECT CONCAT('There are total ', COUNT(Occupation), ' ',LOWER(Occupation),'s.') AS total FROM Occupations
GROUP BY Occupation
ORDER BY total);
但是它没有ORDER BY总。
Ashley(P)
Samantha(A)
Julia(D)
Britney(P)
Maria(P)
Meera(P)
Priya(D)
Priyanka(P)
Jennifer(A)
Ketty(A)
Belvet(P)
Naomi(P)
Jane(S)
Jenny(S)
Kristeen(S)
Christeen(S)
Eve(A)
Aamina(D)
There are total 4 actors.
There are total 3 doctors.
There are total 7 professors.
There are total 4 singers.
如果我只运行
SELECT CONCAT('There are total ', COUNT(Occupation), ' ',LOWER(Occupation),'s.') AS total FROM Occupations
GROUP BY Occupation
ORDER BY total
它顺序:
There are total 3 doctors.
There are total 4 actors.
There are total 4 singers.
There are total 7 professors.
一个侧面说明:'UNION'是从结果中删除重复项。你甚至有重复?该查询没有提示。可能你可以用简单的'UNION ALL'来完成,从而节省执行时间。 –