mysql的行转列

select * from ta;
+——+——+——-+
| id | num | value |
+——+——+——-+
| 17 | 1 | 2 |
| 17 | 2 | 3 |
| 17 | 3 | 4 |
+——+——+——-+

select id,max(IF(num = 1,value,0)) as ‘1’ ,max(IF(num = 2 ,value,0)) as ‘2’ ,max(IF(num = 3,value,0)) as ‘3’ from ta group by id;
+——+——+——+——+
| id | 1 | 2 | 3 |
+——+——+——+——+
| 17 | 2 | 3 | 4 |
+——+——+——+——+

你可能感兴趣的:(mysql)