1.逻辑运算符的补充
between 的用法:(在....之间)
select column1,column2,......columnN from 表名
where columnX between 第一个值 and 第二个值;
示例:select name,age from customers where age between 26 and 30;
SQL中逻辑运算符的优先级列表
级别 操作符
1 ~(Bitwise NOT)
2 * 乘法 /除法 %取模
3 + 加号 -减号 &(Bitwise AND) ^(Bitwise Exclusive OR)
| (bitwise OR)
4 =,>,<,>=,<=,<>,!=,!<,!>比较操作符
5 NOT
6 AND
7 ALL,ANY,BETWEEN,IN,LIKE,OR
8 = (赋值)
注意:sql中可以使用圆括号()来控制优先级
update更改:
update 表名 set column1=value1,column2=value2,....,columnN=valueN
where 条件;
示例:
update customers set age=29 where name="david";
注意
update customers set age=29;
没有where条件语句,意味着将表中所有行的age改为29.
delete删除操作:(删除符合条件的一行)
delete from 表名 where 条件;
示例:
delete from customers where id=1;
注意:
delete from customers;不写where条件语句,结果是把表中的所有记录全部删除
like语句
通配符为%:0,1,或更多
通配符为_:单一数字或者单一字符
示例:
select * from 表名 where 列名 like "XXXX%"
select * from 表名 where 列名 like "_XXXX_"
思考:
select * from customers where salary like "_5__.__";
select * from customers where salary like "3%";
select * from customers where salary like "1_000%";
select * from customers where address like "%zh__";
limit限制:(选取的是符合列的属性)[limit语句永远放在最后面]
select * from 表名 limit n;其中n指返回表中的前n条记录
select * from 表名 limit m,n;其中m指记录开始的索引号,第一条记录代码是0,n是指从第m条记录开始,取n条记录(索引也是从0开始)
示例:
select * from customers limit 3; select name,age(可以是一个也可以是多个) from customers limit 3;
select * from customers limit 3,2;
order by 排序:
ASC 升序
DESC 降序
select 列名 from 表名 [where 条件] order by 列名 ASC;
示例:
select name,age from customers order by age ASC;
注意;
order by 后面出现的列名 不必出现在select语句后
练习:
把customers表中,城市为深圳的顾客,按年龄升序,工资降序排列,并且只返回结果中的第一条记录
select * from customers where address="shenzhen" order by age ASC limit 1;
elect * from customers where address="shenzhen" order by salary DESC limit 1;
select * from customers where address="shenzhen" order by age ASC,salary desc limit 1;
[先按age升序排列如果age一样的情况下在按salary降序排列,没有的表中的顺序就是按age排的]
select * from customers where address="shenzhen" order by age ASC,salary desc ;(先按age排序在按salary排序)
Group by 分组:
group by 用来与聚合函数(比如 count总数,sum求和,avg平均值,min最小值,max最大值)联合使用,得到一个或多个列的结果集
语法:
select column1,column2,....columnN,聚合函数(表达式) from 表名 where 条件
group by column1,column2,....columnN
order by column1,column2,....columnN;
注意:******
1.group by之后的列必须出现在select语句之中
2.group by语句必须在where语句之后,order by语句之前
select address,max(salary) from customers group by address;
select max(salary) from customers group by address;[这个做法是没有多大意义的只会出现salary的数据不会出现其他数据]
练习:
列出各个城市的平均工资,按照平均工资由小到大的排序,并且只返回前三条记录
select address,avg(salary) from customers group by address order by avg(salary)asc limit 3;
注意:
因为聚合函数通过作用与一组数据而只返回一个单个值,因此,在select语句中出现的元素要么为一个聚合函数的输入值,要么为group by语句的参数,否则会出错
例如如下语句会报错:
select address,avg(salary)[聚合函数的输入值],name from customers group by address;
where语句后不能直接跟聚合函数,如下例子为错误:
select address,avg(salary) from customers where avg(salary)>4000 group by address order by avg(salary)asc limit 3;
Having语句:(加的是聚合函数)
Having语句通常与group by语句联合使用,用来过滤由group by语句返回的记录集
Having语句的存在弥补了where关键字不能与聚合函数联合使用的不足
语法如下:
select column1,column2,....colulmnN,聚合函数(表达式)from 表名 where 条件[判断条件的语句不能加聚合函数,聚合函数只能加在having后面]
group by column1,column2,....columnN having 条件1,条件2,....,条件N;
示例:
select address,avg(salary) from customers group by address having avg(salary)>4000;
练习:
分别针对除了北京市以外的其他城市,列出年龄大于25岁的顾客的最高收入,并且该最高收入不得低于3000,将这些信息按照工资的升序排列
select address,max(salary) from customers where age>25 and (not address="beijing") group by address having max(salary)>=3000
order by max(salary) asc;
distinct 不同的
与select 一起使用,除去重复项,提前唯一的记录项目
select distinct column1,column2,.....columnN from 表名 where 条件
示例:
select distinct age from customers;
select count(distinct age) from customers;