1
andyhu 2014-06-20 21:50:43 +08:00
select * from help as h order by (select count(*) from help where help.order=h.order) desc
|
2
andyhu 2014-06-20 21:51:32 +08:00
order字段记得做索引
|
3
andyhu 2014-06-20 21:59:00 +08:00
不过感觉需求上有点不太对劲,楼主的需求应该用group by可以搞定的,不一定非要顺序列出所有的id,换group by后性能应该有所提升
|
5
andyhu 2014-06-20 23:49:00 +08:00
@yangqi 可以运行的,试试就知道了。效率低倒是真的,一般不会这么写
CREATE TABLE IF NOT EXISTS `help` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `order` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB; INSERT INTO `help` (`id`, `order`) VALUES (1, 2), (2, 2), (3, 2), (4, 1), (5, 1), (6, 3); |
6
smblog 2014-06-21 01:02:09 +08:00
select id,order from (select count(*) as OrderC,id from help group by id) tt order by tt.OrderC desc
|
7
smblog 2014-06-21 01:06:46 +08:00
写错了。。不过大概就是这样的思路。。
|
8
Ransford OP |