日常笔记19/3/04-19/3/10

一:19/3/5   周二

1): mysql之IF表达式和NULL值

*  通过java查询mysql的NULL值,java用空对象来接受mysql的null值会有空指针隐患

*  对于null的处理 >>>> 在java中用空对象判断在mysql服务器用 IS NULL来判断()

*  对于NULL值的大小判断>>>> NULL值是最小的的值(即order by(默认升序) 排序会排在最前边)

mysql> SELECT *  FROM tb_key ORDER BY NAME;
+----+--------+
| id | name   |
+----+--------+
|  1 | NULL   |
|  4 | NULL   |
|  8 | 'null' |
|  7 | -1     |
|  6 | 1      |
|  3 | 2      |
|  2 | 3      |
+----+--------+
7 rows in set (0.00 sec)

*  若用Mysql服务的IF函数处理null ,if函数在order by(排序)后处理,即只能改变值的显示,并不能影响排序。

mysql> SELECT IF(NAME IS NULL,-1,NAME) FROM tb_key ORDER BY NAME;
+--------------------------+
| IF(NAME IS NULL,-1,NAME) |
+--------------------------+
| -1                       |
| -1                       |
| 'null'                   |
| -1                       |
| 1                        |
| 2                        |
| 3                        |
+--------------------------+
7 rows in set (0.00 sec)
View Code

2): java之可变参数

前言:因为需求缘故,需设计一个方法,该参数可能是一个数组。因为ThinkInJava这本书提到的可变参数提醒我,是否可以用可变参数来设置这个方法,很遗憾最终结果是可变参数不太实用。但借此也做复习一下可变参数。

定义如下:

public class VariableParamter {
    public static void main(String[] args) {
        VariableParamter vm = new VariableParamter();
        vm.tParamter(22,2,66,2,21); //调用可变参数
        int[] a = {2,4,3};
        vm.tParamter(a); //兼容数组
        vm.tParamter(); //可传递空对象,相比数组参数  
        //vm.tParamter2(); //数组参数不能设置空对象
    }
    private void tParamter(int ... arg) {
        System.out.println(arg.toString()); //输出“[I@6d06d69c”  故可推测传过来就是一个数组对象
        for (int i : arg) {
            System.out.println(i);
        }
    }
    private void tParamter2(int[] arg) {
        System.out.println(arg.toString()); //输出“[I@6d06d69c”  故可推测传过来就是一个数组对象
        for (int i : arg) {
            System.out.println(i);
        }
    }
    //不支持泛型
    //private void tParamter3(T... arg) {}
    //如果需要多个参数,多参必须定义放在最后
    private void tParamter4(int a ,int ...agr) {}
    /**
     * tParamter5(int ...agr,int a ) 编写报错如下
     * The variable argument type int of the method tParamter5 must be the last parameter
     */
    //private void tParamter5(int ...agr,int a ) {}
}
View Code

*  可变参数vs数组 >>>可变参数是1.5的新特征,并且兼容数组,即可变参数方法可接受数组,如下

*  可变参数的本质>>>就是数组,

*  如果是多个种类的参数方法,可变参数只能放在最后定义。

*  可变参数不支持泛型

*  可变参数可以传递空对象

3): 实用SQL语句

* 情景1:给旧表添加新字段,并且根据之前的数据,设置不同的值 。

CREATE TABLE `test` (
  `a` int(11) DEFAULT NULL,
  `b` varchar(1024) DEFAULT NULL,
  `c` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
View Code

语句:update test set c= (CASE a WHEN 1 THEN 'one'  WHEN 2 THEN 'two' ELSE 'more' END)

分析:set  col_name = value  其中value值可以是一个表达式,并且该表达式中的字段可以获取到当前的其它值,例如 a和1,2。

注意:update语句不能和select语句连用

* 情景2: 根据业务,需要新建一张关联表。该表关联已知两个表的数据

分析:可以实用insert [into] table_name(col_name1,col_name2........) from .......语句

例如:

猜你喜欢

转载自www.cnblogs.com/jinliang374003909/p/10467341.html