作者hasio227 (hasio)
看板Database
标题Re: [SQL ] 如何找出频率最高的查询一问
时间Mon May 26 21:15:33 2008
※ 引述《fantasywater (狂想)》之铭言:
: ※ 引述《flakchen (flak)》之铭言:
: : 少了 where T1.dno=T2.dno
: : 因为少了那一句的关系
: -----------------------------------------------------
: 结果如下:
: mysql> Select Distinct T1.dno,T1.Salary
: : -> From employee as T1
: : -> Where salary=(select T2.salary
: : -> from employee as T2
: where T1.dno=T2.dno
: : -> group by T2.dno
: : -> order by count(*) desc
: : -> limit 1);
: +------+--------+
: | dno | Salary |
: +------+--------+
: | 5 | 30000 |
: | 1 | 55000 |
: | 4 | 43000 |
: +------+--------+
: 3 rows in set (0.00 sec)
: --------------------------------------------------------
: 会取到下面箭头所指示的那三笔资料
: 里面的 order by count(*) desc 不知为何似乎没有作用到
: 而取到最少出现的那几笔资料
: +------+--------+
: | dno | salary |
: +------+--------+
: | 5 | 30000 | <---
: | 5 | 25000 |
: | 5 | 25000 |
: | 5 | 38000 |
: | 1 | 55000 | <---
: | 4 | 43000 | <---
: | 4 | 25000 |
: | 4 | 25000 |
: +------+--------+
第一个可以用暂存资料表写出来~
<MS-SQL>
select dno,salary,count(salary)'频率'
into #table1
from #table
group by dno,salary
go
select dno,salary,频率
from #table1 t1
whereA 频率=(select max(频率) from #table1 where t1.dno =t2.dno)
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.127.194.122
1F:推 fantasywater:这麽久了居然还有人回!! 05/26 23:24