SQL中常使用的知识点总结
-
普通查询语句
1.select column_name1,column_name2 from table_name;或SELECT * from table_name; 2.当属性列存在多个相同值时,希望查询结果只返回其中的一个 select distinct column_name from table_name; 3.where 子句用于提取那些满足指定条件的记录 select column_name1,column_name2 from table_name where column_name operator value; 4.and或or select * from user where name="q" and age=10.0 ; select * from user where name="1" and (name="2" or age=11.) 5.对结果集进行排序order by 默认是asc升序,desc代表降序 select column_name,column_name from table_name ORDER BY column_name,column_name asc|desc; order by 多列的时候,先按照第一个column name排序,在按照第二个column name排序 6.对查询结果进行选取指定数目的记录 select * from table_name where column_name=value1 limit num 7.模糊查询like,其中_代表匹配一个字符,%代表匹配任意个字符 select * from table_name where column_name like "_value1_" select * from table_name where column_name like "%value1" 使用not关键字查询不匹配的记录 select * from table_name where column_name not like "%_value1" 8.mysql中的正则查询regexp select * from user where name regexp '[qo]';匹配name中含有q或o的记录 select * from user where name regexp '^[qo]';匹配name中以q或o开头的记录 select * from user where name regexp '^[^zo]';匹配name不以z或o开头的记录 9.in允许在where子句中规定多个值,包含的意思 select * from user where name in ("c","java");查询name中含有c或java的记录 10.between选取介于两个值之间的值 select * from user where age between 0 and 100; 选取在两个值之外的值 select * from user where age not between 0 and 100; 11.取别名 select name n from user select name as n from user select u.id ,m.name from user1 as u ,user as m where u.id=4 and m.name="java" 12.联合查询 select * from user1 where sid > (select id as sid from user where name="java")
-
where语句中常用的运算符
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Vz36L27j-1600998524729)(C:\Users\zzj\AppData\Roaming\Typora\typora-user-images\1584883190054.png)]
-
插入数据
1.insert into向数据中插入数据 insert into table_name values(value1,value2,..) a.必须严格按照数据库中属性列的顺序 b.必须给出全部属性列的值,除非是自动生成的 insert into table_name(column1,column1,..) values(value1,value2,..)
-
更新数据
1.update语句更新表中的记录 update table_name set column1=value1 ,column2=value2,..where some_column=some_value a.当没有指定条件时,将更新全部的数据
-
删除数据
1.delete语句用于删除表中的行 delete from table_name where some_column=some_value a.当没有指定条件时,将把表中的数据全部删除
-
join连接操作
-
把两个表或多个表格的行结合起来
-
inner join : 如果表中有至少一个匹配,则返回行 ,inner join==join
select user1.`name` ,`user1`.age,`user`.`name` from user join user1 on `user`.id=user1.sid; 查询的过程 a.逐个取user中id与user1表中的全部sid比较,相等,则返回当前行 b.user表中的id遍历完,返回查询结果集
-
left join: 从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为 NULL
select `user1`.`name`,user1.sid,`user`.`name` from user1 left join user on user1.id=11 and user1.sid=`user`.id a.返回左表user1中全部记录 b.只有在user1.id=11且user1.sid=user.id时,user.name才有值,其他时候都为空
-
right join: 从右表(table2)返回所有的行,即使左表(table1)中没有匹配。如果左表中没有匹配,则结果为 NULL。
select user1.`name`,user1.sid,`user`.`name` from user right join user1 on user1.id=11 and user1.sid=`user`.id a.返回右表user1中全部记录 b.只有在user1.id=11且user1.sid=user.id时,user.name才有值,其他时候都为空
-
-
join中的on与where的区别:
- on 条件是在生成临时表时使用的条件,它不管 on 中的条件是否为真,都会返回左边表或右边表中的记录
- where 条件是在临时表生成好后,再对临时表进行过滤的条件
-
union操作符合并两个或多个 select语句的结果
-
union内部的每个 select语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 select 语句中的列的顺序必须相同。
-
默认地,union 操作符选取不同的值。如果允许重复的值,请使用 union all。
-
union结果集中的列名总是等于 union中第一个 select 语句中的列名
-
使用union命令时需要注意,只能在最后使用一个order by命令,是将两个查询结果合在一起之后,再进行排序!绝对不能写两个order by命令。
select name from user1 where age>=88 union select name from user1 where age>90 order by name desc
select name from user1 where age>=88 union all select name from user1 where age>90 order by name desc
-
-
mysql中常使用的函数
1.avg()返回属性列的平均数 select avg(age) from user1 2.count()统计属性列的数目 select count(1) from user1 where id>2 3.max()返回属性列的最大值 select max(age) as max1 from user1 where age<88 4.min()返回属性列的最小值 select min(age) from user1 5.sum()对属性列进行求和 select sum(age) from user1 where age>60 6.group by语句用于结合聚合函数,根据一个或多个列对结果集进行分组。 select sex,sum(1) as num from user1 group by sex 7.having子句可以让我们筛选分组后的各组数据 select sex,sum(1) as num from user1 group by sex having sum(1)>4 8.exists运算符用于判断查询子句是否有记录,如果有一条或多条记录存在返回 True,否则返回 False。 select * from user1 where name="xiaohong" and exists (select * from user1 where id=4) 相当于 select * from user1 where name="xiaohong" and id=4
select * from user1 where name=“xiaohong” and exists (select * from user1 where id=4)
相当于
select * from user1 where name=“xiaohong” and id=4