我有一个像这样的结构表:
ID NAME SURNAME EXTRA TYPE
______________________________________________
1 MARIO ROSSI RED 10
2 MARCO VERDI YELLOW 10
3 GIANNI BLU TEACHER 20
4 LUCA BLU STUDENT 20
5 LUCA ROSSI GREEN 10
6 MARIA GIALLA 10/08/05 30
7 MARTA ROSA 11/01/79 30
8 FRANCO NERO BARMAN 20
9 MARY NERI 05/09/88 30
10 MAX BLU 06/08/98 30
现在我需要按名称,姓氏和(额外[DESC],其中type = 30)对查询进行排序,所以对于第三个条件,我的意思是我会按降序排列类型为30的所有额外数据:
....
6 MARIA GIALLA 10/08/05 30
10 MAX BLU 06/08/98 30
9 MARY NERI 05/09/88 30
7 MARTA ROSA 11/01/79 30
....
所以我试过这个:
select * from my_table order by name, surname, if(my_table.type=30, extra desc, extra asc)
但它给了我一个Sql错误。
感谢您的支持。
感谢其他程序员,根据我的数据,我用这种方式解决了:ORDER BY name,surname,if(TYPE = 30,STR_TO_DATE(EXTRA,'%d /%m /%Y%H.%我'),null)DESC
答案 0 :(得分:1)
尝试此查询: -
Architecture
答案 1 :(得分:1)
更有用的是,请考虑以下事项:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,NAME VARCHAR(12) NOT NULL
,SURNAME VARCHAR(12) NOT NULL
,EXTRA VARCHAR(12) NOT NULL
,TYPE INT NOT NULL
);
INSERT INTO my_table VALUES
( 1,'MARIO','ROSSI','RED','10'),
( 2,'MARCO','VERDI','YELLOW','10'),
( 3,'GIANNI','BLU','TEACHER','20'),
( 4,'LUCA','BLU','STUDENT','20'),
( 5,'LUCA','ROSSI','GREEN','10'),
( 6,'MARIA','GIALLA','2005/08/10','30'),
( 7,'MARTA','ROSA','1979/01/11','30'),
( 8,'FRANCO','NERO','BARMAN','20'),
( 9,'MARY','NERI','1988/09/05','30'),
(10,'MAX','BLU','1998/08/06','30'),
(11,'MARIO','ROSSI','PLUMBER','20'),
(12,'MARCO','VERDI','TAILOR','20'),
(13,'GIANNI','BLU','YELLOW','10'),
(14,'LUCA','BLU','BLUE','10'),
(15,'LUCA','ROSSI','BAKER','20'),
(16,'MARIO','ROSSI','2004/08/10','30'),
(17,'MARCO','VERDI','1978/01/11','30'),
(18,'FRANCO','NERO','RED','10'),
(19,'FRANCO','NERO','1987/09/05','30'),
(20,'MARIA','GIALLA','1995/08/06','30');
SELECT name
, surname
, MAX(CASE WHEN type = 10 THEN extra END) colour
, MAX(CASE WHEN type = 20 THEN extra END) occupation
, MAX(CASE WHEN type = 30 THEN extra END) date
FROM my_table
GROUP
BY name
, surname
ORDER
BY name
, surname
, extra
, type
, date DESC;
+--------+---------+--------+------------+------------+
| name | surname | colour | occupation | date |
+--------+---------+--------+------------+------------+
| FRANCO | NERO | RED | BARMAN | 1987/09/05 |
| GIANNI | BLU | YELLOW | TEACHER | NULL |
| LUCA | BLU | BLUE | STUDENT | NULL |
| LUCA | ROSSI | GREEN | BAKER | NULL |
| MARCO | VERDI | YELLOW | TAILOR | 1978/01/11 |
| MARIA | GIALLA | NULL | NULL | 2005/08/10 |
| MARIO | ROSSI | RED | PLUMBER | 2004/08/10 |
| MARTA | ROSA | NULL | NULL | 1979/01/11 |
| MARY | NERI | NULL | NULL | 1988/09/05 |
| MAX | BLU | NULL | NULL | 1998/08/06 |
+--------+---------+--------+------------+------------+
您可以加入这样的结果(或实际上更简单的替代方案 - 但我现在仍然坚持这一点)回到原始表格,以便按照您的喜好订购结果。我在STR_TO_DATE函数中添加了仅供参考。显然,这里没有必要,与你的版本需要的方式略有不同......
SELECT x.*
, y.date
FROM my_table x
LEFT
JOIN
( SELECT name
, surname
, MAX(CASE WHEN type = 30 THEN extra END) date
FROM my_table
GROUP
BY name
, surname
) y
ON y.name = x.name
AND y.surname = x.surname
ORDER
BY x.name
, x.surname
, STR_TO_DATE(y.date,'%Y/%m/%d') DESC;
+----+--------+---------+------------+------+------------+
| ID | NAME | SURNAME | EXTRA | TYPE | date |
+----+--------+---------+------------+------+------------+
| 18 | FRANCO | NERO | RED | 10 | 1987/09/05 |
| 19 | FRANCO | NERO | 1987/09/05 | 30 | 1987/09/05 |
| 8 | FRANCO | NERO | BARMAN | 20 | 1987/09/05 |
| 13 | GIANNI | BLU | YELLOW | 10 | NULL |
| 3 | GIANNI | BLU | TEACHER | 20 | NULL |
| 14 | LUCA | BLU | BLUE | 10 | NULL |
| 4 | LUCA | BLU | STUDENT | 20 | NULL |
| 15 | LUCA | ROSSI | BAKER | 20 | NULL |
| 5 | LUCA | ROSSI | GREEN | 10 | NULL |
| 17 | MARCO | VERDI | 1978/01/11 | 30 | 1978/01/11 |
| 2 | MARCO | VERDI | YELLOW | 10 | 1978/01/11 |
| 12 | MARCO | VERDI | TAILOR | 20 | 1978/01/11 |
| 6 | MARIA | GIALLA | 2005/08/10 | 30 | 2005/08/10 |
| 20 | MARIA | GIALLA | 1995/08/06 | 30 | 2005/08/10 |
| 11 | MARIO | ROSSI | PLUMBER | 20 | 2004/08/10 |
| 16 | MARIO | ROSSI | 2004/08/10 | 30 | 2004/08/10 |
| 1 | MARIO | ROSSI | RED | 10 | 2004/08/10 |
| 7 | MARTA | ROSA | 1979/01/11 | 30 | 1979/01/11 |
| 9 | MARY | NERI | 1988/09/05 | 30 | 1988/09/05 |
| 10 | MAX | BLU | 1998/08/06 | 30 | 1998/08/06 |
+----+--------+---------+------------+------+------------+
答案 2 :(得分:0)