日期:2014-05-16  浏览次数:20912 次

求一个SQL,排序或分组
数据如下:
id name date?
1 a 11-11
2 b 11-12
3 a 11-13
4 c 11-10
5 a 11-15
6 d 10-15
7 c 11-20
8 d 12-20
9 c 10-13
10 a 10-15
要求结果如下:
id name date sum
8 d 12-20 2
6 d 10-15 2
7 c 11-20 3
4 c 11-10 3
9 c 10-13 3
5 a 11-15 4
3 a 11-13 4
1 a 11-11 4
10 a 10-15 4
2 b 11-12 1

------解决方案--------------------
真看不出有什么排序规律,自己再补排序方式吧
SQL code

SELECT id, name AS new_name, date, (

SELECT count( * ) 
FROM test1
WHERE name = new_name
)sum
FROM test1
ORDER BY name

------解决方案--------------------
SQL code

SELECT id, NAME AS name1, DATE,(SELECT COUNT(NAME) FROM tb WHERE NAME = name1) AS SUM
FROM tb
ORDER BY NAME DESC,DATE DESC;

------解决方案--------------------
SQL code
select * from table1 a
order by (select max(date) from table1 where name=a.name)

------解决方案--------------------
SELECT *,(SELECT COUNT(*) FROM ttl WHERE a.`name`=`name` ) AS `sum`
?FROM ttl a
ORDER BY FIND_IN_SET(a.`name`,'d,c,a,b'),a.`date` DESC