SQL基础
- select :查
语法:select * from 表名称 - insert into : 增
语法:insert into 表( 字段 ) values( 对应字段的值 ) - delete :删
语法:delete from 表 where 字段 = 值 - update :改
语法:update 表 set 字段 = 新值 where 字段 = 值; - where :条件
语法: select * from 表 where id = 1;(查询id为1的所有字段) - and & or :相当于Java中的 与 和 或
语法 :select * from 表 where id = 1 and age = 3;(查询 id 为 1,且 age 为 3 的字段) - distinct :去重
语法:select distinct name from 表(查询所有 name 不同的数据) - order by :排序
语法:select * from 表 order by age(按年龄升序排序)
ASC:升序(没写,默认为升序)
DESC:降序
SQL函数
- avg:平均
语法:select avg(字段名) from 表 - count:获取行数
语法:select count(字段名) from 表 - sum:和
语法:select sum(字段名) from 表 - max:最大
语法:select max(字段名) from 表 - min:最小
语法:select min(字段名) from 表 - group by :分组
语法:select * from 表 group by 字段名 - having:合计函数不能和where一起使用
语法:select * from 表 having sum(字段) > 1000
SQL高级
- like:可以配合着 % , _ 使用
语法:select * from 表 where name like ‘%张%’ - between:取两个的中间值
语法:select * from 表 where age between 10 and 30 - in:where规定多个值
语法:select * from 表 where name in(‘zs’,‘ls’) - join(inner join):通过多表查询要的内容
语法:select 字段 from 表1 join 表2 on 表一字段 = 表二字段 order by id - left join:左查询(就算右表对应数据为空,一样显示空)
语法:select 字段 from 表1 left join 表2 on 表一字段 = 表二字段 order by id - right join:右查询(就算左表对应数据为空,一样显示空)
语法:select 字段 from 表1 right join 表2 on 表一字段 = 表二字段 order by id