0%

数原 4.4 数据更新

插入数据

insert into tbname values(val1,val2,...),(val21,val22,...),...

可以插入多条记录

insert ... set

只能插入一条记录

insert ... select

把子查询的结果集插入表中。

删除数据

delete from tbname where

更新数据

update tbname set col1=val1,col2=val2,... where

select case when condition1 then expr1 when condition2 then expr2 else expr3 end as colalias

聚合函数:

如果使用了group by ,结果是多条记录,即多个分组,每个分组的聚合。

如果没有group by ,结果是一条记录,是对所有记录的聚合。

四、Where 子句和条件查询

1. 比较运算

  • 两个值都不为NULL:结果为 TRUE /FALSE
  • 其中一个值为NULL或都为NULL:比较结果为 UNKNOWN
  • <=>:两个值相等,或,都为NULL,结果为 TRUE,否则 FALSE ### 2. 范围
  • between ... and :闭区间 , [ ]
  • in (枚举)

3. 判断是不是空值

  • colname IS [NOT] NULL

4. 子查询

select * from tb_student where studentno in (select studentno from tbscore where score>=80)

select * from tb_student stu inner join tb_score sco on stu.studentno = sco.studentno where sco.score>80

expression <比较运算符> all | any | some (subquery)

  • all : expr 需要与子查询的每个结果进行比较
  • some/any: 只要子查询有值满足比较,即可。

exist (subquery): 判断 subquery 的结果集是否为空。

五、group by 子句,分组数据

select * from customers group by

group by 是对 select 选择的列组成的结果集,再做分组。

-------------本文结束,感谢您的阅读-------------