MySQL與Oracle中分組、聚合函數(shù)的區(qū)別 |
發(fā)布時間: 2012/8/11 17:40:09 |
今天需要這樣一句sql:先用group by進行分組,然后利用聚合函數(shù)count 或者sum進行計算,并顯示其它的輔助信息。 在MySQL環(huán)境中,我模擬如下環(huán)境: CREATE TABLE `room` ( `rid` varchar(5) default NULL, `rname` varchar(5) default NULL, `pid` int(11) default NULL, `seq` int(11) NOT NULL auto_increment, PRIMARY KEY (`seq`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 情景:人住房間, 統(tǒng)計某個房間某個人住的次數(shù)
用戶表,客人的信息 `ID` int(11) NOT NULL auto_increment, `USERNAME` varchar(50) default '', `PASSWORD` varchar(50) default '', PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk Mysql中語句如下: select count(u.username),r.rname,r.rid,r.pid from room r,user1 u where r.pid=u.id group by r.rid,r.pid 這里r.rname并沒有出現(xiàn)在group by子句、聚合函數(shù)中,但是MYSQL中仍然能夠執(zhí)行、列出數(shù)據(jù)。 /* --顯示:Ora-00979 not a ORDER BY expression --因為: order by 后邊的c.channel_code不在ORDER BY子句中 select count(c.channel_name),m.media_name from channel c,media m where c.media_code = m.media_code group by c.media_code,m.media_name order by c.channel_code
--顯示:Ora-00979 not a GROUP BY expression --因為:group by 或者聚合函數(shù)中沒有包含c.channel_name select count(c.channel_name),m.media_name,c.channel_name from channel c,media m where c.media_code = m.media_code group by c.media_code,m.media_name */ --通過: select count(c.channel_name),m.media_name from channel c,media m where c.media_code = m.media_code group by m.media_name --正常 select count(c.channel_name),m.media_name from channel c,media m where c.media_code = m.media_code group by c.media_code,m.media_name
--正常 select count(c.channel_code),m.media_name from channel c,media m where c.media_code = m.media_code group by c.media_code,m.media_name 本文出自:億恩科技【mszdt.com】 |