常用sql语句总结

一、更新      

update <表名> set <列名=更新值> [where <更新条件>]

二删除

1.删除表中的行

语法:
     delete from 表名称 where 列名称 = 值;     
             ---->  delete from user where name='feifei';

2.删除所有行,不删除表结构索引等
 语法:
     delete from 表名称;   
             ---->  delete from user;

三、常用查询语句

1、查询所有字段
     语法:
     select 字段1,字段2,...from 表名;     
             ---->  select tbt.id,tbt.typeName,tbt.typeDesc from t_bookType tbt;
      select * from 表名;           
             ---->  select * from t_bookType;

2、查询指定字段
     语法:
      select 字段1,字段2,...from 表名;     
             ---->  select tbt.id,tbt.typeName from t_bookType tbt;

3、where条件查询(*可以是指定的一些字段,下同)
       语法:      
       select * from 表名 where  条件表达式 (字段=指定值);
             ---->  select * from t_book where author = '陈帅荣';

4、带in关键字的查询
       语法:
        select * from 表名 where 条件表达式(字段 [not] in (指定值1,指定值2,...));
              ---->  select * from t_book where bookTypeId in (1,2);

5、带between and查询
       语法:
       select *  from 表名 where 条件表达式(字段 [not] between 指定值1 and 指定值2);
              ---->  select * from t_book t where t.price between 100.0 and 130.0;

6、带like的模糊查询('%'代表任意个,'_'代表一个)
       语法:
       select *  from 表名 where 条件表达式(字段 like 指定值);
              ---->  select * from t_book t where t.bookName like '%java%';

              ---->  select * from t_book t where t.author like '陈帅_';

 

7、空值查询
     语法:
     select * from 表名 where 条件表达式(字段 is [not] null);
              ---->  select * from t_book t where t.price is not null;

8、带and的多条件查询
     语法:
     select * from 表名 where 条件表达式1 and 条件表达式2 and条件表达式3  ...;
              ---->  select * from t_book t where t.author = '陈帅荣' and t.price is not null;
              ---->  select * from t_book t where t.author = '陈帅荣' and t.price >120.0;

9、带or的多条件查找
     语法:
     select * from 表名 where 条件表达式1 or 条件表达式2 or 条件表达式3  ...;
              ---->  select * from t_book t where t.author = '陈帅荣' or t.price is null;
              ---->  select * from t_book t where t.author = '陈帅荣' or t.price >120.0;

10、distinct去重复查询
     语法:
     select distinct 指定字段 from 表名;
              ---->  select t.author from t_book t;//这个是不去重复,相同的值也会显示出来
              ---->  select distinct t.author from t_book t;//这个是去重复,相同的值只显示一次

11、对查询结果排序(order by)
     语法:
     select * from 表名[where  条件表达式] order by 字段 排序方式(升序asc、降序desc)
              ---->  select * from t_book order by price asc;
              ---->  select * from t_book where author = '陈帅荣' order by price desc;

12、group by 分组查询
    语法:group by 字段 [having 条件表达式][with rollup]
    1、单独使用(查询结果毫无意思,不使用)
    2、与group_concat()函数一起使用
            ----> select bookTypeId,group_concat(author) from t_book group by bookTypeId;
    3、与聚合函数一起使用
            ----> select bookTypeId,count(bookName) from t_book group by bookTypeId;
    4、与having函数一起使用(限制输出的结果)
            ----> select bookTypeId,count(author) from t_book group by bookTypeId having count(author)<3;
    5、与with rollup一起使用(最后一行会输出统计后的记录数据)
            ----> select bookTypeId,count(bookName) from t_book group by bookTypeId  with rollup;
            ----> select bookTypeId,group_concat(author) from t_book group by bookTypeId with rollup;

13、limit 分页查询
    语法:
    select * from 表名 limit 起始记录,每页显示的记录数;
             ----> select * from t_book limit 0,3;//显示第一页三条记录
             ----> select * from t_book limit 3,3;//显示第二页三条记录
             ----> select * from t_book limit 6,3;…
 函数
 
1、count()函数:求表的总记录数
       ----> select count(*) as total from t_book;
       ----> select author,count(*) as total from t_book group by author;//和group by 一起使用

2、sum()函数:求和运算
      ----> select sum(price) from t_book where bookTypeId = '1';
      ---->(5.0版本的错误查询) select author,sum(price) from t_book where author= '陈帅荣';//当条件指定某一字段时,前面查寻使用聚合函数时不能查看某一列的值,只有在group by语句中可以查看某一列,如下:不过mysql数据在5.0以上的版本中使用上面的语句也能执行。
      ----> select author,sum(price) from t_book group by author;//和group by 一起使用

3、avg()函数:求平均值运算
      ----> select avg(price) from t_book where author= '陈帅荣';
      ----> select author,avg(price) from t_book group by author;//和group by 一起使用

4、max()函数:求最大值运算
      ----> select max(price) from t_book where author= '陈帅荣';     
      ----> select author,max(price) from t_book group by author;//和group by 一起使用

5、min()函数:求最小值运算
      ----> select min(price) from t_book where author= '陈帅荣';     
      ----> select author,min(price) from t_book group by author;//和group by 一起使用

一、连接查询:连接查询可以查询两张及两张以上的表之间的数据。
    
        有张表: a表有7条记录,b表有5条记录,那么a表和b表的笛卡尔积是:7*5=35
         ----> select * from a,b;//输出结果为35条数据

  1、内连接查询:查询结果是左右连接的交集【即左右连接的结果去除null项后的并集(去除重复项)】
         ----> select * from t_book t1,t_bookType t2 where t1.bookTypeId = t2.id;

  2、外连接查询:
        1、左外连接:以左边表为基准,匹配右边表的记录,符合条件的查询出来,同时基准表的所有数据都会查出
        ----> select * from t_book t1 left join t_bookType t2 on t1.bookTypeId = t2.id;

        2、右外连接:同左外连接,只是以右边的表为基准
        ----> select * from t_book t1 right join t_bookType t2 on t1.bookTypeId = t2.id;

       ## a表 左外连接 b表  和  b表 右外连接 a表  查询出的结果一致。

 3、带多条件连接查询:在连接查询的基础上添加一个或多个条件
       ----> select * from t_book t1 left join t_bookType t2 on t1.bookTypeId = t2.id where t1.price < 100.0;

    注意:* 号可以指定一些字段,有的时候为了提高效率,往往只查看需要的字段。

二、子查询:就是讲某个查询结果作为另一查询语句的条件进行判断再来查询
  1、带not in/in 关键字
        ----> select * from t_book t1 where t1.price in (select t2.price from t_level t2);
        ----> select * from t_book t1 where t1.price not in (select t2.price from t_level t2);

  2、带比较运算符的子查询
         ----> select * from t_book t1 where t1.price >= (select t2.price from t_level t2 where t2.level =1);

  3、带not exists/exists 关键字
         exists 后面的语句返回的是true/false ,如果为真就执行前面的语句,否则就不执行语句。
         ----> select * from t_book t1 where exists (select t2.price from t_level t2);
         ----> select * from t_book t1 where not exists (select t2.price from t_level t2);

  4、带 any 关键字
         any 后面的条件满足任意一种就可以
         ----> select * from t_book t1 where t1.price  >= any (select t2.price from t_level t2);

  5、带 all 关键字
       all 后面的条件必须全部满足(类似于取条件中的最大或最小数据)
        ----> select * from t_book t1 where t1.price  >= all (select t2.price from t_level t2);

三、union 合并查询
         union:查询去重复的数据
                      ---->  select t1.id from t_book t1 union select t2.id from t_bookType t2;
         union all:查询带重复的数据
                      ---->  select t1.id from t_book t1 union all select t2.id from t_bookType t2;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值