各位前輩好,
想請教關於SQL的問題,這是一個投票的資料表
courseid=課程,cmid=課數(EX:第一課、第二課),vote=課程滿意度(有1,2,3)
以下是資料表
CREATE TABLE mdl_block_point_view
(id
bigint(10) NOT NULL,courseid
bigint(10) NOT NULL,cmid
bigint(10) NOT NULL,userid
bigint(10) NOT NULL,vote
tinyint(1) NOT NULL
)
我想要寫指令將資料合併:
希望他可以用SQL語法自動統計出
第一課==>8人滿意
第一課==>5人普通
第二課==>9人不滿意....
但我的語法不知道哪裡錯了,滿意和不滿意的人數都會融合再一起,請問各位高手該怎麼改比較好呢?select DISTINCT count(A.vote),A.vote,A.cmid from mdl_block_point_view A where A.courseid='4' group by A.cmid
CREATE TABLE `mdl_block_point_view` (
`id` bigint(10) NOT NULL,
`courseid` bigint(10) NOT NULL,
`cmid` bigint(10) NOT NULL,
`userid` bigint(10) NOT NULL,
`vote` tinyint(1) NOT NULL
) ;
INSERT INTO `mdl_block_point_view` (`id`, `courseid`, `cmid`, `userid`, `vote`) VALUES
(2, 3, 8, 3, 3),
(3, 3, 18, 3, 3),
(4, 3, 12, 3, 1),
(7, 4, 11, 2, 3),
(8, 4, 10, 3, 2),
(9, 4, 9, 3, 1),
(10, 3, 7, 3, 1),
(11, 2, 1, 3, 2),
(12, 2, 2, 3, 2),
(13, 3, 17, 3, 2),
(14, 3, 7, 4, 1),
(15, 3, 8, 4, 1),
(16, 3, 18, 4, 3),
(17, 2, 4, 2, 2),
(18, 2, 3, 2, 1),
(19, 2, 6, 2, 3),
(20, 2, 5, 2, 1),
(21, 4, 19, 2, 2),
(22, 4, 10, 2, 3),
(23, 4, 9, 2, 1);
select A.courseid,A.cmid,A.vote,count(A.vote)
from mdl_block_point_view A
where A.courseid='4'
group by A.courseid,A.cmid,A.vote
order by A.courseid,A.cmid,A.vote;