数据库SQL常用操作语句

155 阅读4分钟

1. 基础增删改查

  1. 查询user表所有数据:

    select * from user

  2. 查询user表中name字段所有数据:

    select name from user

  3. 查询user表中符合条件的所有name字段:

    select name from user where password in (he,hong,qian)

  4. 增加一条数据:

    insert into user (id,name,passowrd) values (1,hehongqian,123456)

  5. 删除一条数据:

    delete from user where id=1

  6. 修改一条数据

    update user set name=何红乾 where id=1

  7. 分页查询(page页数,count页码)

    select * from user limit page,count

  8. 分页排序查询 (page页数,count页码, 按name排序)

    select * from user where 1=1 order by name limit page,count

  9. 根据name字段进行分页排序查询:

    select * from user where 1=1 order by name desc limit page,count

    select * from user where 1=1 order by name asc limit page,count

2.条件操作

  1. 批量查询

    select * from user where name in(1,2,3,4,5)

  2. 批量删除

    delete from user where id in(1,2,3,4,5)

  3. 批量修改

    update user set name='hehongqian' where id in(1,2,3,4,5)

  4. 查询最前面2行数据

    select top 2 * from user

  5. like查询(%代表缺少的字符,可以是很多字母组成)

    select * from user where name like 'h%'

    select * from user where name like '%h%'

  6. ”_“通配符查询(_代表缺少的一个字母)

    select * from user where name like '_on'

  7. 查询name以H,K,N开头的数据

    select * from user where name like '[HKN]%'

  8. 查询name不以H,K,N开头的数据

    select * from user where name like '[!HKN]%'

  9. 查询指定范围的数据(左包右不包)

    select * from user where name between 'he' and 'qian'

  10. 查询指定范围外的数据

    select * from user where name not between 'he' and 'qian'

  11. 指定别名查询

    select * from user u where u.name='he' and u.name='hong' and u.name='qian'

3.多表查询

  1. 多表联查(user表,person表)

    select user.name,person.name from person,user where person.id=user.id

  2. join 内连接多表联查(person表中没有没有匹配user表的id,就不会返回该行数据)

    select user.name,person.name from user inner join orders on user.id=person.id order by user.name

  3. join 左连接多表查询(person表中没有匹配user表的id,也会返回该行数据,以user表为主,不对应的字段为null)

    select user.name,person.name where user left join person on user.id=person.id order by user.name

  4. join 右连接多表查询 (person表中没有匹配user表的id,也会返回该行数据,以person表为主,不对应的字段为null)

    select user.name,person.name where user right join person on user.id=person.id order by user.name

  5. join 全连接多表查询 (只要有一个id存在,就返回该行数据)

    select user.name,person.name where user full join person on user.id=person.id

  6. 多表合并查询(返回所有数据,包括重复的)

    select name from user union all select name from person

  7. 多表合并查询(返回不重复的数据)

    select name from user union select name from person

  8. 从一个表将指定数据插入到另一个表

    select * into new_user from user

  9. 将另一个数据库的user表数据插入到user表中

    select * into user in 'aa.mdb' from user

3.SQL函数

  1. 查询表中某个字段最大值,取名为max_id

    select max(id) max_id from user

  2. 查询表中某个字段最小值

    select min(id) from user

  3. 查询表中最后一行某个字段的值

    select last(name) from user

  4. 查询表中第一行某个字段的值

    select first(name) from user

  5. 查询表中某个字段的列总和

    select sum(id) from user

  6. 查询所有不同name的在表中某个字段的总数量

    select name,sum(id) from user group by name

  7. 查询所有不同name的在表中某个字段的总数量( 并且总数量小于200)(having出现是由于where和合计函数不能同时使用)

    select name,sum(id) from user group by name having sum(id)<200

  8. 将name字段的值改成大写

    select ucase(name) as name,password from user

  9. 将name字段的值改成小写

    select lcase(name) as name,password from user

  10. 从name字段中提取前三个字符

    select mid(name,1,3) as name from user

  11. 获取name字段长度

    select len(name) from user

  12. 将A表中price舍去小数位

    select round(price,0) as price from A

  13. 获取当前数据库时间

    select now() from user

  14. 格式化查询的数据(A表:name String,price String)

    select name ,price, format(now(),'YYYY-MM-DD') as now_time from A