易截截图软件、单文件、免安装、纯绿色、仅160KB

mysql分组排序方案

mssql,oracle中
test表:
1 5 abc
2 6 bcd
1 7 ade
2 8 adc
select   a,b,c
from(
select   a,b,c
,row_number()over(partition   by   a   order   by   b   desc)   rn
from   test
)  
where   rn=1
由于MYSQL没有提供类似ORACLE中OVER()这样丰富的分析函数. 所以在MYSQL里需要实现这样的功能,我们只能用一些灵活的办法:
例1
1.首先我们来创建实例数据:
drop table if exists heyf_t10;
create table heyf_t10 (empid int ,deptid int ,salary decimal(10,2) );
insert into heyf_t10 values
(1,10,5500.00),
(2,10,4500.00),
(3,20,1900.00),
(4,20,4800.00),
(5,40,6500.00),
(6,40,14500.00),
(7,40,44500.00),
(8,50,6500.00),
(9,50,7500.00);
2. 确定需求: 根据部门来分组,显示各员工在部门里按薪水排名名次.
显示结果预期如下:
+-------+--------+----------+------+
| empid | deptid | salary | rank |
+-------+--------+----------+------+
| 1 | 10 | 5500.00 | 1 |
| 2 | 10 | 4500.00 | 2 |
| 4 | 20 | 4800.00 | 1 |
| 3 | 20 | 1900.00 | 2 |
| 7 | 40 | 44500.00 | 1 |
| 6 | 40 | 14500.00 | 2 |
| 5 | 40 | 6500.00 | 3 |
| 9 | 50 | 7500.00 | 1 |
| 8 | 50 | 6500.00 | 2 |
+-------+--------+----------+------+
3. SQL 实现
select empid,deptid,salary,rank from (
select heyf_tmp.empid,heyf_tmp.deptid,heyf_tmp.salary,@rownum:=@rownum+1 ,
if(@pdept=heyf_tmp.deptid,@rank:=@rank+1,@rank:=1) as rank,
@pdept:=heyf_tmp.deptid
from (
select empid,deptid,salary from heyf_t10 order by deptid asc ,salary desc
) heyf_tmp ,(select @rownum :=0 , @pdept := null ,@rank:=0) a ) result
;
4. 结果演示
mysql> select empid,deptid,salary,rank from (
-> select heyf_tmp.empid,heyf_tmp.deptid,heyf_tmp.salary,@rownum:=@rownum+1 ,
-> if(@pdept=heyf_tmp.deptid,@rank:=@rank+1,@rank:=1) as rank,
-> @pdept:=heyf_tmp.deptid
-> from (
-> select empid,deptid,salary from heyf_t10 order by deptid asc ,salary desc
-> ) heyf_tmp ,(select @rownum :=0 , @p


相关文档:

为powerdesigner添加mysql的字符集支持

一般建模可能都会用到:powerdesigner
但是,在建表的时候,我一直没有找到:
DEFAULT CHARACTER SET
COLLATE
两个选项。因此,想了个方法,点击:工具栏-》database-》edit current DBMS
然后,选中:MYSQL50::Script\Objects\Table\Options
在options末尾添加:
ENGINE = %s : list = BDB | HEAP | ISAM | InnoDB ......

mysql主从配置一些注意

配置主从:
两台服务器mysql版本一样,
Server version: 5.1.16-beta-log MySQL Community Server
配置基本一样、启动参数一样
主的可以访问从的,
但是
从那边怎么连接主的,都是失败:
[root@2006 htmlfile]# mysql -h192.168.xx.xxx -uxxx -pxxx --default-character-set=GBK --reconnect
ERROR 2003 (HY000): C ......

基于MySQL数据库的UTF8中文网站全文检索的实现


http://hi.baidu.com/xiyouwang/blog/item/9f15bbee99b0ce202df5349e.html
现在的互联网上,很多网站都提供了全文搜索功能,浏览者可以通过输入关键字或者是短语来搜索特定的资料。在PHP+MySQL构架的网站中,通常的做法是通过SELECT查询的Like语句来进行搜索,这一办法存在搜索不够精确、以及效率非常低下的缺点。比如对 ......

主流数据库MYSQL/MSSQL/ORACLE测试数据库脚本代码

/******************************************************************************/
/*
主流数据库MYSQL/MSSQL/ORACLE测试数据库脚本代码
脚本任务:建立4个表,添加主键,外键,插入数据,建立视图
运行环境1:microsoft sqlserver 2000 查询分析器
运行环境2:mysql5.0 phpMyAdmin网页界面
运行环境3:oracle 9i SQL*PLU ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号