MySQL與Oracle中分組、聚合函數(shù)的區(qū)別 |
發(fā)布時(shí)間: 2012/9/24 17:16:00 |
今天需要這樣一句sql:先用group by進(jìn)行分組,然后利用聚合函數(shù)count 或者sum進(jìn)行計(jì)算,并顯示其它的輔助信息。 在MySQL環(huán)境中,我模擬如下環(huán)境: 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 情景:人住房間, 統(tǒng)計(jì)某個(gè)房間某個(gè)人住的次數(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 --因?yàn)椋?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 --因?yàn)椋篻roup 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】 服務(wù)器租用/服務(wù)器托管中國五強(qiáng)!虛擬主機(jī)域名注冊(cè)頂級(jí)提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM] |