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

使用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
可以看到多出了汇总信息


相关文档:

mysql 中的字符串连接 CONCAT(str1,str2,...)

返回来自于参数连结的字符串。如果任何参数是NULL,返回NULL。可以有超过2个的参数。一个数字参数被变换为等价的字符串形式。
mysql> select CONCAT('My', 'S', 'QL');
-> 'MySQL'
mysql> select CONCAT('My', NULL, 'QL');
-> NULL
mysql> select CONCAT(14.3);
-> '14.3'
如:update test set ......

MySQL小数位取整

//得分计算四舍五入
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 ......

Java+Mysql的数据库查找实现


public class select {
 public List XiuGai_select(String keyword){
  List list=new ArrayList();
        Connection conn = null;
  Statement stmt = null;
  String sql=null;
  ResultSet res = null;
  get ......

Mysql外键约束

1.myisam表不支持外键,innodb才支持外键
2.mysql外键的列类型和主表中相应的主键列类型必须一致,即类型一致、长度一致,否则会报错。ERROR 1005: Can't create table (errno: 150)
3.在一个数据库中外键名字不能重复,否则会报ERROR 1005: Can't create table 'XXXXXX' (errno: 121) ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号