查询数据

来源:互联网 发布:net域名后缀排名高吗 编辑:程序博客网 时间:2024/04/24 17:55

1.基本查询语句

   select 属性列表

                 from 表名和数据列表

                 [where 条件表达式1]

                 [group by 属性名1 [having 条件表达式2]]

                 [order by 属性名2 [asc|desc]]

  属性列表:需要查询的字段名

  表名和数据列表:从此处指定的表或者视图中查询数据,表和视图可有多个

  条件表达式1:查询条件

  属性名1:按该字段中的数据进行分组;

  条件表达式2:满足该表达式的数据才能输出

  属性名2:按该字段数据进行排序;默认asc.


2.单表查询

2.1查询所有字段

(1)列出表的所有字段

(2)使用”*“查询所有字段

           select * from 表名

2.2查询指定字段

  select 字段1,字段2 from 表名

2.3查询指定记录

      where 查询条件

查询条件查询条件符号或关键字比较=、<、<=、>、>=、!=、<>、!>、!<指定范围between and、not between and指定集合in、not in匹配字符like、not like是否为空值is null、is not null多个查询条件and、or2.4带in关键字的查询

   [not] in (元素1,元素2,...,元素n)

2.5带between and的范围查询

   [not] between 取值1 and 取值2             !!大于等于”取值1“小于等于”取值2“

2.6带like的字符匹配查询

   [not] like '字符串'

  !!字符串可以是一个完整的字符串,也可以是包含“%”或“_”的通配字符

  !!“%”可以代表任意长度的字符串;"_"只能代表单个字符    !!匹配一个汉字需要两个"_",即"_ _".

2.7查询空值

  is [not] null

2.8带and的多条件查询

  条件表达式1 and 条件表达式2 [and ...]

2.9带or的多条件查询

  条件表达式1 or 条件表达式2 [or ...]

!! or 可以和 and 一起使用。此时,and 要比 or 先运算

2.10 查询结果不重复

  select distinct 属性名

2.11对查询结果排序

  order by 属性名 [asc|desc]             

   !!空值(null)最小;可以指定按多个字段排序,先按第1个字段排序,相等时在按第2个字段排序(order by 字段1,字段2)

2.12分组查询

  group by 字段名 [having 条件表达式][with rollup]

  !! with rollup:会在所有记录最后加上一条记录,该记录是上面所有记录的总和

  (1)单独使用 group by 分组

      只显示一个分组的一条记录

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

     select 字段1,group_concat(字段2) from 表名 group by 字段1

      !!每个分组中指定的字段值(字段2)都会显示出来

   (3)与集合函数一起使用

      可以通过集合函数计算分组中的总记录、最大值、最小值等。

     count():统计记录的条数

     sum():计算字段的值得总和     !! 只能计算数值类型的字段; 计算字符字段时,结果都为0

     avg():计算字段的值得平均值

     max():查询字段最大值      !! 也可计算字符(按对应的ASCⅡ码)

     min():查询字段最小值      !! 也可计算字符(按对应的ASCⅡ码)

     例: select 字段1,count(字段1) from 表名 group by 字段1

          

    (4)与having一起使用

         例:select 字段,count(字段) from 表名 group by 字段 having count(字段)<num

        

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

          select * from 表名 group by 字段1,字段2

     (6)与with rollup一起使用

       例:

 2.13用 limit 限制查询结果的数量    !! MYsql特有

      (1)不指定初始位置

              limit 记录数             !! 若“记录数”大于总记录数,则显示所有记录

       (2)指定初始位置

             limit 初始位置,记录数             !! 第一条记录数是0

 

3.使用集合函数查询

 

4.连接查询

    4.1内连接查询

        微笑例:

select num,name,employee.d_id,age,sex,d_name,function from employee,department where employee.d_id=department.d_id;

    4.2外连接查询

         select 字段列表 from 表名1 left|right join 表名2on 表名1.字段1=表名2.字段2

         左连接查询(left):可查询出“表名1”中所有记录,“表名2”中只能查询出匹配记录

         右连接查询(right):可查询出“表名2”中所有记录,“表名1”中只能查询出匹配记录

    4.3复合条件连接查询

        

5.子查询

 5.1带 in 关键字的子查询

      select * from 表1 where 字段 [not] in(select 字段 from 表2)

 5.2带比较运算符的子查询

      select 字段列表 from 表1 where 字段[比较运算符](select 字段 from 表2 where 条件)

    例:select id,name,score from computor_stu where score>=(select score from scholarship where level=1)

 5.3带exists关键字的子查询

      select * from 表1 where exists(select 字段 from 表2 条件)          !! exists()返回一个真假值

 5.4带any关键字的子查询

      any表示满足其中任一条件。通常与比较运算符一起使用

      例:select * from computer_stu where score>=any(select score from scholarship)

 5.5带all关键字的子查询

      all表示满足所有条件。通常与比较运算符一起使用。

      例:select * from computer_stu where score>=all(select score from scholarship)


6.合并查询结果

       select 语句1 union|union all select 语句2 union|union all ...

       !! union会去除掉重复的记录,union all会显示所有记录


7.为表和字段取别名

7.1为表取别名

     表名 表的别名

    例:select * from department d where d.d_id=1001

7.2为字段取别名

     属性名 [as] 别名

  例:select d_id as department_id,d_name as department_name from department

          !!在查询条件中不能使用新定义的字段别名

7.3同时为表和字段取别名

 例:selectd.d_id as department_id,d.d_name as department_name,d.function,d.address fromdepartment d whered.d_id=1001


8.使用正则表达式查询

    属性名 regexp '匹配方式'

    属性名:需要查询的字段的名称;

 正则表达式的模式字符正则表达式的模式字符含义^匹配字符串开始部分$匹配字符串结束部分.代表字符串中任意一个字符,包括回车和换行[字符集合]匹配其中任意一个字符[^字符集合]匹配除了其中以外任意一个字符S1|S2|S3匹配其中任意一个字符串*代表多个该符号之前的字符,包括0和1个+代表多个该符号之前的字符,包括1个字符串{N}字符串出现N次字符串{M,N}出现至少M次,至多N次

8.1查询以特定字符或字符串开头的记录

       例:

select * from info where name regexp '^L'
!! 查询name字段以字母“L”开头的记录

8.2查询以特定字符或字符串结尾的记录

例:select * from info where name regexp 'c$'

8.3用“.”代替字符串中任意一个字符

例:以“L”开头,以“y”结尾,中间有任意两个字符

select * from info where name regexp '^L..y$'

8.4匹配指定字符中任意一个

例1:

select * from info where name regexp '[ceo]'
例2:
select * from info where name regexp '[a-z0-9]'

8.5匹配指定字符以外的字符

例:

select * from info where name regexp '[^a-w0-9]'

8.6匹配指定字符串

例:

select * from info where name regexp 'ab|uc|ic'

8.7使用“*”和“+”来匹配多个字符

例1:c之前出现过0或者多个a

select * from info where name regexp 'a*c'!! Jack、abc12、Lucy
例2:c之前出现过多个a
select * from info where name regexp 'a+c'
!! Jack、abc12

8.8使用{M}或{M,N}来指定字符串连续出现的次数

例1:

select * from info where name regexp 'a{3}'
!! baaabc、baaaabc、baabc

例2:

select * from info where name regexp 'ab{1,3}'
!! cabc、cabababc、cababababc


0 0