MySQL與Oracle中分組、聚合函數的區別 |
發布時間: 2012/9/24 17:16:00 |
今天需要這樣一句sql:先用group by進行分組,然后利用聚合函數count 或者sum進行計算,并顯示其它的輔助信息。 在MySQL環境中,我模擬如下環境: CREATE TABLE `room` ( -
`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 情景:人住房間, 統計某個房間某個人住的次數
用戶表,客人的信息 `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并沒有出現在group by子句、聚合函數中,但是MYSQL中仍然能夠執行、列出數據。 /* --顯示: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 或者聚合函數中沒有包含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
本文出自:億恩科技【www.vbseamall.com】 |