zoukankan      html  css  js  c++  java
  • Mysql--查询基础

    sql查询

    1.Where约束条件

    1. select * from table where id >=1 and id <=9;
    2. select * from table where id between 3 and 9;
    3. select * from table where id in [1,2,3];
    4. select * from table where id not in (1,2,3)
    5. select * from table where  id
    6 . select * from table where post not Null;
    通配符:
    7. select * from table where name like ‘%s’
    8. select * from table where name like '____'  #查询名字为4个的
    	select * from table where char_length(name) = 4;
    	
    

    2.group_by

    分组使用的聚合函数: max,min,avg,count,sum
    1. select avg(id) from table;  默认为一组
    2. select count(salary) from table group_by 部门;
       分组的时候只能拿到,分组的依据,以及聚合函数的使用
    就是要拿到其他字段怎么办
    分组后使用的group_concat(name,":",salary)  中间可以拼接;
    
    分组前使用的 concat('name:',name) as 别名 
    
    插曲:as的用法,起别名,可以给字段,也可以给表
    select name as myname form table
    其中as可以省略  
    select name myname from table #这种语意不明确,不建议使用
    

    3.having

    select sum(salary) from table where id >10 group_by 部门 having sum(salary) >10;
    

    4.where group_by having 的前后顺序

    where >group_by> having
    注意:
        聚合函数不能跟在where之后
    

    5.distinct去重

    select distct * fom table;不能去重,有id,很容易忽视的点
    select distict name from table; #可以去重
    

    6.limit 分页

    selesct * from table limit 5;  #拿五个
    selesct * from table 0,5;#从第0个开始,往后取5条;
    					 5,5; #
    

    7.正则

    select * from table where name=regexp '^l.*(n|y)$';
    

    8.连表

    select * from table,table2;  #笛卡尔积;
    
    select * from table inner join table2;
    
    select * from table left join table2;
    
    select * from table right join table2;
    
    select * from table; union select *from table2;
    

    9.复制表

    create table new_table select name from old_table;  #查询结果为一张虚拟表,可以保存起来,仅仅是有数据,没有外键;
    

    10.数据展示

    select * from tableG; #当数据多的时候,用这个展示一行行
    

    11.跨表查询的两种方式;

    1.连表:
        把表连起来,然后再查;
    2.子查询:
        先查询一张表,把这张表当作其他查询的依据;
    

    补充

    1.case when min(num) < 10 Then 0 ELSE min(num)
    
    2.if(isnull(name),'无名氏',name)
    

    sql查询过关1

    sql查询过关题2

    永远不要高估自己
  • 相关阅读:
    Linux与Windows命令的比较
    操作系统的启动过程
    Spyder快捷键
    pytoch的最佳打开方式
    操作系统逻辑结构
    插值法
    bzoj3509[CodeChef]COUNTARI
    bzoj2969 矩形粉刷
    hdu5575 Discover Water Tank
    bzoj3473字符串&bzoj3277串
  • 原文地址:https://www.cnblogs.com/liqiangwei/p/14410568.html
Copyright © 2011-2022 走看看