zoukankan      html  css  js  c++  java
  • 表记录的检索(数据查询)

    select 语句的基本语法

    SELECT selection_list            #要查询的语句
    FROM 数据表名                      #指定数据表
    WHERE primary_constraint   #查询是需要满足的条件
    GROUP BY grouping_columns     #如何对结果进行分组
    ORDER BY sorint_cloumns            #如何对结果进行排序
    HAVING secondary_constraint      #查询是满足的第二条件
    LIMIT count

    (1)使用select语句查询一个数据表  *代表所有列

    select * from tb_manager;

    (2)查询表中的一列或多列

    select id,name from tb_manager;

    (3)从多个表中获取数据

     select tb_bookinfo.id,tb_bookinfo.bookname,tb_booktype.typename,tb_bookinfo.price

    from tb_booktype,tb_bookinfo;

    二、单表查询

    (1)查询所有字段

    select * from 表名;

    (2)查询指定字段

    select 字段名 from 表名;

    (3)查询指定数据

     后要加一个查询条件  where 子句。

     

     运算符
    名称   实例 运算符  名称  实例 
     =  等于  id=5  is not null  是否为空  id is not null
    > 大于  id>5   between  是否在某区间  id between 1 and 15
    < 小于  id<5   in 在某些固定值中   id in(3,,4,5)
    >=  大于等于  id>=5   not in  不在某些固定值中  name not in (shi,li)
    <= 小于等于  id<=5   like  模式匹配  name like ('shi%')
    !=或<>  不等于   id!=5  not like 模式匹配 name not like ('shi%') 
    is null   是否为空 id is null  regexp  正则表达式匹配  name regexp 正则表达式

    实例:select * from tb_manager where name='mr';

    (4)带In的关键字查询

    select bookname,author,price from tb_bookinfo where bookcase in(4,6);

    (5)带between and的范围查询

    select * from 表名 where 条件 between  值1 and  值2;

    select * from tb_borrortime where borrowtime between '2017-02-01' and '2017-02-28';

    或not between

    (6)带like的字符匹配查询:两种通配符:"%"和“_”

    %可匹配一个或多个可为0,_只匹配一个

    select * from tb_bookinfo where bookname like 'Java%';

    (7)带and的多条件查询

    使用and时只有同时满足才会被查询出来

    select * from 数据表名 where 条件1 and 条件2 and 条件3;

    select * from tb_manager where name='mr' and pwd='mrsoft';

    (8)带or的多条件查询

    or关键字只要满足条件中的一个

    (9)用distinct关键字去除结果中的重复行

    select distinct 字段名 from 表名;

    (10)用order by 对结果进行排序

    order by 字段名   asc|desc;

    asc升序  desc降序

    select * from tb_borrow order by borrowTime by desc;

    (11)用group by 分组查询

    select 字段名,count(*) from 表名 group by 字段名;

    select bookid,count(*) from tb_borrow group by bookid;

    bookid count(*)
    7 2
    8 1

    (2)group by 与group_concat()函数一起使用

    可以将每组中的所有字段都显示出来

    select bookid,group_concat(readerid) from tb_borrow group by bookid;

    bookid group_concat(readerid)
    7 4,4
    8 5

    (3)按多个字段进行分组

    先按第一个字段进行分组,当第一个字段有相同值时,再按第二字段进行分组,以此类推。

    select bookid,readerid from tb_borrow group by bookid,rederid;

    bookid readerid
    7 4
    8 6
    9 5
    9 6
    10 6

    (13)用limit限制查询结果的数量

    例:实现查询最后被劫的3本图书,按借阅时间进行降序排列,显示前三条记录;

    select * from tb_borrow1 order by  borrowTime desc limit 3;

    select * from tb_borrow1 order by  borrowTime desc limit 2,3;从编号2 开始,查询3条记录;

  • 相关阅读:
    LeetCode113. 路径总和 II
    LeetCode257. 二叉树的所有路径
    LeetCode222. 完全二叉树的节点个数
    LeetCode404. 左叶子之和
    LeetCode110. 平衡二叉树
    LeetCode101. 对称二叉树
    LeetCode100. 相同的树
    llustrator CC2017下载AI2020
    vs code 代码格式化整理
    人生格言
  • 原文地址:https://www.cnblogs.com/shanpao/p/7668156.html
Copyright © 2011-2022 走看看