美文网首页
数据库SQL常用操作语句

数据库SQL常用操作语句

作者: 莫问前程F6 | 来源:发表于2021-08-13 15:27 被阅读0次

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:页数-1,offset:一页的数据)

select * from user limit #{page},#{offset}

8. 分页排序查询 (page:页数-1,offset:一页的数据,根据name排序)

select * from user where 1=1 order by time desc limit #{page},#{offset}

9. 根据传过来的字段amount进行分页排序查询:

select * from user where 1=1 order by

<if test="amount == 'desc'">

name desc

</if>

<if test="amount == 'asc'">

name asc

</if>

limit #{page},#{offset}

10. 批量查询

    select * from user where name in

    <foreach item="name" collection="list" separator="," open="(" close=")" index="">

      #{name}

    </foreach>

11. 批量删除

     delete from user where id in

    <foreach item="id" collection="list" separator="," open="(" close=")" index="">

      #{id}

    </foreach>

12. 批量修改

      update user set name='hehongqian' where id in

    <foreach item="id" collection="list" separator="," open="(" close=")" index="">

      #{id}

    </foreach>

13. 查询最前面2行数据

select top 2 * from user

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

select * from user where name like 'h%'

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

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

select * from user where name like '_on'

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

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

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

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

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

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

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

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

20. 指定别名查询

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

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

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

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

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

23. 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

24. 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

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

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

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

select name from user union all select name from person

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

select name from user union select name from person

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

select * into new_user from user

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

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

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

select max(id) max_id from user

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

select min(id) from user

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

select last(name) from user

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

select first(name) from user

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

select sum(id) from user

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

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

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

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

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

select ucase(name) as name,password from user

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

select lcase(name) as name,password from user

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

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

40. 获取name字段长度

select len(name) from user

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

select round(price,0) as price from A

42. 获取当前数据库时间

select now() from user

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

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

相关文章

  • MySQL数据库编写SQL语句利器---mycli

    MySQL数据库的操作是利用SQL语句完成SQL语句的操作。 对于初学者,学习SQL语句是操作数据库的必经之路,但...

  • Android数据库ORM框架:GreenDAO使用简介

    Android最常用的数据库是SQLite,通常使用SQLite进行CRUD操作需要记住一些常用的SQL语句,这不...

  • 数据库SQL语言入门(二)

    系列文章: 数据库SQL语言入门(一)数据库SQL语言入门(三) DDL语句 常用的SQL语句关键字有 creat...

  • MySQL --Section one

    SQL常用的语句1.1 MySQL的基本操作1. 开启MySQL数据库服务 格式: net start mys...

  • iOS进阶第二天 (数据库)

    一、数据库管理系统 二、SQL语句 三、 iOS的数据库技术实现 数据库常用概念: SQL:SQL是Structu...

  • iOS-数据库-SQL

    iOS-数据库 -sql 先从数据库语句-SQL语句 1、在程序运行过程中,要想操作(增删改查,CRUD)数据库中...

  • 2.SQL语句介绍

    1. 关系型数据库的常见组件 2. SQL语言规范 3. SQL语句分类 4. SQL语句构成 5. 数据库操作 ...

  • SQL 注入的注释方法

    常用的数据库有 Microsoft SQL Server 和 MySQL。 SQL 语句注释方法有/* */ 和-...

  • SQL之WHERE语句

    SQL语句是数据库查询语句,可以应用在各种数据库操作软件中,比如Mysql,Oracle,因此SQL语句学一套就基...

  • 数据库

    SQL语句增删改查 SQL表关系 SQLiteDatabase操作SQLite数据库 SQLiteOpenHelp...

网友评论

      本文标题:数据库SQL常用操作语句

      本文链接:https://www.haomeiwen.com/subject/cnadbltx.html