企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# SQL Cookbook ## 查找空值 查找空值切记不能用`=`操作, 会返回`Empty Set` ~~~ mysql> select * from emp where comm is null; ~~~ ## 空值转换为实际值返回 > 改变输入的形式, 但并不改变表中的数据 coalesce()函数有1个或者多个参数, comm非空时返回comm, null时返回0, 也可以用`case语句判断实现` ~~~ mysql> select coalesce(comm, 0) from emp; ~~~ ## 从一个表中查找与其他表不匹配的记录 `外连接` ~~~ mysql> select d.* from dept d left outer join emp e -> on (d.deptno = e.deptno) where e.deptno is null; ~~~ ## 插入更新删除 ## 插入技巧 ## 从一个表向另外的表中复制行 解决方案: 在insert语句后面紧跟一个用来产生索要插入行的查询 ~~~ mysql> create table dept_east( -> deptno int, -> dname varchar(30), -> loc varchar(30)); mysql> insert into dept_east(deptno, dname, loc) -> select deptno, dname, loc from dept -> where loc in('new york', 'boston'); ~~~ ## 赋值表定义 > 只复制已有表的定义, 不复制其中的记录, 创建表的时候, 使用`一个不返回任何行的子查询, where的条件时钟为false` ~~~ mysql> create table dept_2 -> as -> select * from dept where 1 = 0; ~~~ ## 修改技巧 ~~~ mysql> update emp set sal = sal * 1.10 where deptno = 20; ~~~ ## 删除技巧 ### 删除所有记录 ~~~ mysql> delete from dept_2; ~~~