mysql 学习笔记

1.查询不同的值:

     distinct :此关键字指示mysql只返回不同的值。

  用法:select distinct fieldname from  tablename;

2.限制查询结果:

    limit 5:指示mysql返回不多于5行。

    用法:select fieldname from tablename limit 5;

    limit 5,5:指示mysql返回从行5开始的5行。第一个数为开始位置,第二个数为要检索的行数。

    用法:select fieldname from tablename limit 5,5;

   limit 4 offset 3:意为从3行开始取4行,就像 limit 3,4。

3.使用完全限定的表名:

  用法:select tablename.fieldname from databasename.tablename;

4.排序数据:

      order by:取一个或多个列的名字,据此对输出进行排序。

  用法:1.select fieldname from tablename order by fieldname;

     2.select fieldname1, fieldname2 from tablename order by fieldname1,fieldname2; #首先按fieldname1排序,然后按fieldname2排序。

5. 指定排序方向

    desc:为实现降序排序,必须指定desc关键字

    用法:1.select fieldname from tablename order by fieldname desc;

           2.select fieldname1, fieldname2 from tablename order by fieldname1 desc,fieldname2; #首先按fieldname1降序排序,然后按fieldname2排序。

6.order by 和 limit 组合:

    效果:能够找出一列中最高或最低的值。

    用法: select fieldname from tablename order by filedname desc limit 1;

7.where语句相关操作符:

    =   等于

   <>   不等于

  !=  不等于

  <  小于

    <=    小于等于

      >=    大于等于

      BETWEEN 在指定两个值之间  用法:select fieldname from table between 5 and 10;

8.空值检查

  is null/is not null:检查是否是null,与 0、空字符串或仅仅包含空格不同。

  用法:select fieldname from tablename where fieldname is null;

9.in操作符

  in:操作符用来指定条件范围,范围中每个条件都可以进行匹配。

  用法:select fieldname from tablename where id in (1002,1003) order by fieldname;

10.mysql的模糊匹配:

  1.like操作符:利用通配符"%"可以搜索以下模式:'fieldnamesubstring%'匹配以fieldnamesubstring开头、'%fieldnamesubstring'匹配以fieldname结尾、'%fieldnamesubstring%'匹配含有fieldname,其中'%'表示任何字符出现任意次数。

    用法:select fieldname from tablename where fieldname like 模式;

   2.正则表达式:

               regexp:后所跟东西作为正则表达式。

      用法:select fieldname from tablename where fieldname regexp 正则表达式。

  

原文地址:https://www.cnblogs.com/baoyiluo/p/3183588.html