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

求教句sql
SQL code
CREATE TABLE IF NOT EXISTS `mj_knowledge` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `time` int(11) NOT NULL,
  `subject` varchar(200) NOT NULL,
  `uname` varchar(15) NOT NULL,
  `email` varchar(20) NOT NULL,
  `attached_file` varchar(120) NOT NULL,
  `type` tinyint(4) NOT NULL,
  `sort` int(11) NOT NULL,
  `content` text NOT NULL,
  `status` tinyint(4) NOT NULL DEFAULT '2',
  `uid` char(20) NOT NULL,
  `FAQ_inserted` int(11) NOT NULL DEFAULT '2',
  `stick` tinyint(4) NOT NULL DEFAULT '1',
  `priority` varchar(10) NOT NULL DEFAULT 'normal',
  `charge` varchar(20) NOT NULL,
  PRIMARY KEY (`id`),
  FULLTEXT KEY `subject` (`subject`),
  FULLTEXT KEY `content` (`content`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=22676 ;

我想要得到不同uid的stick总数并按照stick排序?要怎么写。。求教

------解决方案--------------------
我觉得决不应该是这么简单的问题,不敢相信是这样的:
SQL code
select uid, sum(stick) as total
from mj_knowledge
group by uid
order by total

------解决方案--------------------
select uid, sum(stick) as total
from mj_knowledge WHERE stick=2
group by uid
order by total

------解决方案--------------------
mysql> select uid, sum(stick) as total
-> from mj_knowledge WHERE stick=2
-> group by uid
-> order by total;
+----------------------+-------+
| uid | total |
+----------------------+-------+
| 4535 | 2 |
| U2012061200000000185 | 4 |
| 1111111 | 8 |
+----------------------+-------+
3 rows in set (0.00 sec)
------解决方案--------------------
。。。。这个有啥问题啊?
mysql> select sum(stick) from mj_knowledge;
+------------+
| sum(stick) |
+------------+
| 24 |
+------------+
1 row in set (0.00 sec)
mysql> select stick from mj_knowledge where stick=2;
+-------+
| stick |
+-------+
| 2 |
| 2 |
| 2 |
| 2 |
| 2 |
| 2 |
| 2 |
+-------+
7 rows in set (0.00 sec)
mysql> select sum(stick) from mj_knowledge where stick=2;

mysql> select a.uid, a.stick from
-> (
-> select uid uid,sum(stick) stick from mj_knowledge
-> where stick=2
-> group by uid
-> ) a
-> order by a.stick;
+----------------------+-------+
| uid | stick |
+----------------------+-------+
| 4535 | 2 |
| U2012061200000000185 | 4 |
| 1111111 | 8 |
+----------------------+-------+
3 rows in set (0.00 sec)
数量都对得上啊。。。。14
+------------+
| sum(stick) |
+------------+
| 14 |
+------------+
1 row in set (0.00 sec)