MySQL Заказ запроса - дальнейший вопрос - PullRequest
1 голос
/ 11 октября 2010

В дополнение к недавно отвеченному вопросу у меня есть следующий код:

SELECT   q21coding, COUNT(q21coding) AS Count 
FROM     tresults_acme 
WHERE    q21 IS NOT NULL AND q21 <> '' 
GROUP BY q21coding
ORDER BY IF(q21coding = 'Other', 1, 0) ASC, Count DESC

Возвращает следующее:

q21coding                                  Count 
Difficulty in navigating/finding content     53
Positive comments                            28
Suggestions for improvement                  14
Inappropriate content/use                    13
Improve search facility                       6
Include information about staff and teams     5
Content needs updating                        4
Other                                        30

Вы заметите, что Other сейчас находится наbottom - Однако есть ли способ гарантировать, что Положительные комментарии и Другое ВСЕГДА являются двумя нижними (с другим внизу) независимо от размера счета?

Спасибо,

Гомер

1 Ответ

2 голосов
/ 11 октября 2010

На самом деле не было необходимости использовать IF(q21coding = 'Other', 1, 0) в исходном запросе.В MySQL вы можете использовать любое выражение в ORDER BY caluse, и q21coding = 'Other' было бы достаточно:

... ORDER BY q21coding = 'Other', Count DESC

Выражение q21coding = 'Other' вернет 1, если true, или 0, если false,Это поместит строки с q21coding = 'Other' внизу.

То, что вам нужно сделать, чтобы «Позитивные комментарии» и «Другое» внизу было примерно таким:

... ORDER BY q21coding = 'Other', q21coding = 'Positive comments', Count DESC

Базовый контрольный пример:

CREATE TABLE my_table (id int, q21coding varchar(100), count int);

INSERT INTO my_table VALUES (1, 'Inappropriate content/use', 13);
INSERT INTO my_table VALUES (2, 'Other', 30);
INSERT INTO my_table VALUES (3, 'Difficulty in navigating/finding content', 53);
INSERT INTO my_table VALUES (4, 'Positive comments', 28);
INSERT INTO my_table VALUES (5, 'Improve search facility', 6);
INSERT INTO my_table VALUES (6, 'Content needs updating', 4);
INSERT INTO my_table VALUES (7, 'Suggestions for improvement', 14);
INSERT INTO my_table VALUES (8, 'Include information about staff and teams', 5);

Результат:

SELECT    q21coding, count
FROM      my_table
ORDER BY  q21coding = 'Other', q21coding = 'Positive comments', Count DESC;

+-------------------------------------------+-------+
| q21coding                                 | count |
+-------------------------------------------+-------+
| Difficulty in navigating/finding content  |    53 |
| Suggestions for improvement               |    14 |
| Inappropriate content/use                 |    13 |
| Improve search facility                   |     6 |
| Include information about staff and teams |     5 |
| Content needs updating                    |     4 |
| Positive comments                         |    28 |
| Other                                     |    30 |
+-------------------------------------------+-------+
8 rows in set (0.00 sec)
...