MySQL查询语句基础知识

Select的基本语法形式

Select       属性列表

                   From         表名和视图列表

                   [where     条件表达式1]

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

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

使用*查询所有字段:

Select *   from                   表名;

查询指定字段:

Select       属性1 ,属性2,属性4      from                   表名;

查询指定记录:

Where      条件表达式

带in关键字的查询:

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

带between and 的范围查询:

[not]          between           取值1      and            取值2;

带like的字符匹配查询:

[not]          like   ‘字符串’                     (可以是单引号或双引号)

“%”代表任意长度 的字符串,b%k表示以字母b开头以字母k结尾

“_”代表单个字符,b_k表示字母b开头k结尾的三个字符

查询空值:

Is      [not]                   null

带AND(or)的多条件查询:

条件表达式1           and(or)                 条件表达式2

查询结果不重复:

Select       distince    属性名

对查询结果排序:

Order        by     属性名     [asc|desc]        (如果含有空值最先显示)

分组查询:

Group       by     属性名     [Having    条件表达式][WITH          rollup]

单独使用group       by关键字来分组:

Select*     from                   表名         group        by     属性;

Group       by关键字与group _concat()函数一起使用:

Select       属性,group_concat(name)            from                   表名         group        by     属性

Group       by关键字与集合函数一起使用:

Select       属性1,count(属性2)      from                   表名         group        by     属性1

Group by关键字与having一起使用:

Select       属性1,count(属性2)      from                   表名         group by  属性1    having    count(属性2)>=3       (having条件表达式作用于分组后的记录)

按多个字段进行分组:

Select*     from                   表名         group        by     属性1,属性2

Group      by关键字与with    rollup一起使用:

Select       属性1,count(属性1)      from                   表名         group        by     属性1      with Rollup

用limit限制查询结果的数量

不能指定初始位置:

Limit         记录数

指定初始位置:

Limit         初始位置,记录数

使用集合函数查询:

Count()函数

Select 属性1,count(*)         from                   表名         group  by        id;

Sum()函数

Select       属性1,sum(属性2)      from                   表名         where       限制条件

AVG()函数

Select       avg(age)            from        表名

MAX()函数MIN()函数

连接查询

内查询:

Select       属性1,属性2,表名1.属性3,         from                   表名1,表名2 ,          where         表名1.属性1=表名2.属性2

外连接查询

Select       属性名列表     from                   表名1      lift|right            join   表名2

On    表名1.属性名1=表名2.属性名2;

左连接:可以查询出“表名1”所指的表中的所有记录,而“表名2”所指的表中,只能查询出匹配的记录

子查询

带in关键字的子查询:

Select*     from                   表名1      where       属性1      in(select         属性1 from     表名2)

带比较运算符的子查询:

Select       属性1,属性2       from          表名1      where       属性2>=

(select      属性2      from                   表名2      where       限制条件)

带exists关键字的子查询:

Select*     from                   表名1      where       exists(select    属性1      from          表名2    where 限制条件)

带any(all)0关键字的子查询:

Select*     from                   表名1      where       属性1>=any(all)      (select      属性1      from                   表名2)

合并查询结果

Select       语句1      union|union     all

Select       语句2      union|union     all…

Select       语句n;

为表和字段取别名

为表取别名:

表名         表的别名

Select*     from        表名         表的别名

为字段取别名:

属性名     [AS]  别名

Select       d_id AS            department_id

使用正则表达式查询:

关键字regexp                            属性名     regexp      ‘匹配方式’

查询以特定字符或字符串开头(结尾)的记录(^)($):

Select*     from                   表名         where       属性         regexp      ‘^条件’    (‘条件$’)

用字符‘.’来替代字符串中的任意一个字符:

Select*     from                   表名         where       属性         regexp      ‘^l..y$’

匹配指定字符中的任意一个([字符]):

Select*     from                   表名         where       属性         regexp      ‘[字符]’

匹配指定字符以外的字符([^字符]):

Select*     from                   表名         where       属性         regexp      ‘[^字符]’

匹配指定字符串:

Select*     from                   表名         where       属性         regexp      ‘字符|字符’

注意:字符与|之间不能有空格

使用‘*’和‘+’来匹配多个字符:

Select*     from                   表名         where       属性         regexp      ‘a+b’

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

字符串{M}表示字符串连续出现M次

字符串{M,N}表示字符串连续出现至少M次,最多N次

Select*     from                   表名         where       属性         regexp      ‘a{2}’ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值