• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
谢城
博客园    首页    新随笔    联系   管理    订阅  订阅

mysql学习总结(二)

1.索引

       唯一索引和联合唯一索引

          唯一索引:unique(列名) :有添加unique关键字的列名在列名下的数据不会重复,添加unique关键字的列表能够提高查询效率

         联合唯一索引: unique(列名1,列名2,....) 特性是在括号内存在多个列名,它的规则是同表下不能出现相同的一行数据,可以用来建立多对多的数据表

       增:

         copy数据的sql语句:insert into 表名(列名) select 列名 from 另一个表名;

       删:

           delete from 表名 where id =表id and / or   name='xxxxxx';根据条件删除数据

      改:

             update 表名 set name='xxxxx',age='xxxxx'  where  id =表id;

    查询:(重点)

              1.闭区间查询

                        select * from 表名  where id  between 9 and 35; 查询id 9到35之间的数据 

              2.通配符查询:

                           select * from 表名 name like '李%';

              3.排序查询

                      降序: select * from 表名 order by 列名  desc; 

                      升序: select*from 表名 order by 列名  asc;

                       多列升序降序: select * from 表名 order by 列名 desc, 列名 asc;

               4.分组查询:把一组中相同的数据进行排序

                  select age, count(num) from 表名 as 别名 group by age; 

                 select age,count(num) as cnt from 表名 group by age  having cnt >1;

                    having的=实现二次查询;

                   where 和having的区别:

                           1.having与where类似,可以筛选数据

                            2.where针对表中的列表发会作用,查询数据

                            3.having针对查询结果中的列表发挥作用,二次筛选数据,和group by 配合使用

              5.分页查询:

                         select * from 表名 limit (page-1)*offset, offset;

             6.连表操作:

                         左连接:

                               select * from 表1 left join 表2 on 表1.id=表2.id;

                         右连接:

                               select * from 表1 right join 表2  on 表1.id=表2.id

                        内连接:

                                   select * friom 表1 inner join 表2  on 表1.id=表2.id;

 

查询顺序:

      select name,sum(score) from 表     

              1.where id > 10   

              2.group by    score 

              3.having     age> 12     

              4.order by age   desc

              5.limit 2, 10 

 

posted @ 2019-06-13 21:23  谢城  阅读(224)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3