使用mysql中的with rollup得到group by的汇总信息
使用mysql中的with rollup可以得到每个分组的汇总级别的数据:
表如下:
CREATE TABLE `test3` (
`id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`name1` varchar(10) DEFAULT NULL,
`name2` varchar(10) DEFAULT NULL,
`cnt` int(2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1
数据为:
1 rank1 subrank1 1
2 rank1 subrank1 2
3 rank2 subrank1 1
4 rank2 subrank2 2
5 rank3 subrank1 1
6 rank1 subrank2 3
查询(1):
select name1,name2,sum(cnt) from test3 group by name1,name2
得到结果:
rank1 subrank1 3
rank1 subrank2 3
rank2 subrank1 1
rank2 subrank2 2
rank3 subrank1 1
查询(2):
select name1,name2,sum(cnt) from test3 group by name1,name2 with rollup
得到结果:
rank1 subrank1 3
rank1 subrank2 3
rank1 NULL 6
rank2 subrank1 1
rank2 subrank2 2
rank2 NULL 3
rank3 subrank1 1
rank3 NULL 1
NULL NULL 10
可以看到多出了汇总信息
相关文档:
//得分计算四舍五入
SELECT ROUND((SUM(getfeng)/SUM(totalfeng))*100) as feng from answerdata WHERE uid='151' AND targetid IS NOT NULL
1.ceil () /ceiling() 向上取整
例: ceil(1.2) = 2
2.floor () 向下取整
例: floor(1.2) = 1
3.round() 四舍五入
&n ......
1.select max(id) from user;
2.select last_insert_id() as id from user limit 1;
(这个测试的返回id一直是0,有点问题)
3.储存过程
1)
oracel中
create sequence seqID
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
nocache
order;
create or replace procedure ......
MySQL存储过程的参数用在存储过程的定义,共有三种参数类型,IN,OUT,INOUT,形式如:
CREATE PROCEDURE([[IN |OUT |INOUT ] 参数名 数据类形...])
1、IN 输入参数:表示该参数的值必须在调用存储过程时指定,在存储过程中修改该参数的值不能被返回,为默认值
2、OUT 输出参数:该值可在存储过程内部被改变,并可返回
3、I ......
表:
CREATE TABLE `user` (
`id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
`password` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
其中有数据为:
1 name1 123456
2&nbs ......
语法:
select * from table_name where column_name regexp '正则表达式'
或区分大小写
select * from table_name where column_name regexp binary '正则表达式'
支持的正则表达式符号:
. 任意字符
| 或,如:a|b|c
[] 范围,比如:[a-z],[0-9],[^0-9]不包 ......