关于数据库表中的值,各种操作相关的杂七杂八:

一、where 条件

  1、并且:……and……

    select from 表 where  id>1 and  name!='alex'  and  num=12;

  2、区间:between……and……

     select from 表 where id between and 16;

  3、枚举:in……\not in……

    select from 表 where id in (11,22,33);

    select from 表 where id not in (11,22,33);

    select from 表 where id in (select nid from 表);

  4、通配符:%、_

    %:模糊匹配所有,  _:模糊匹配一个,

    select from 表 where name like 'ale%'  - ale开头的所有(多个字符串)

    select from 表 where name like 'ale_'  - ale开头的所有(一个字符)

       

二、分页:limit

  select from 表 limit 5;            - 前5行

  select from 表 limit 4,5;           - 从第4行开始的5行

  select from 表 limit 5 offset 4        - 从第4行开始的5行(同上一条)

      

三、排序:order by

  1、单列:desc按从大到小排列、asc按从小到大排列

    select from 表 order by 列 asc              - 根据 “列” 从小到大排列,默认
    select from 表 order by 列 desc             - 根据 “列” 从大到小排列
  2、多列:出现相同值时,按后面列指定顺序排列,
    select from 表 order by 列1 desc,列2 asc    - 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序

     

四、分组:group by

  1、获取分组信息:select num from  表 group by num

  

   2、获得分组内其他栏目信息:select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num;

  

   3、利用分组汇总后的信息做判断条件:group by ……having……        

  

 五、组合:union

  1、自动去重:select  nickname from A  union select  name from B

  2、不去重:select  nickname from A  union all select  name from B