关于操作数据库的一些笔记

来源:互联网 发布:windows平板镜像下载 编辑:程序博客网 时间:2024/06/06 03:16

数据库

         !=,<>    不等于

         as   别名   select user_name as 用户名 , user_password as 密码 from user


like % \ 的简单应用

         like语句不配合下划线或者百分号就相当于等于
        '%a'      查出以a结尾的记录
          '_a'      查出第二个字符为a的记录    

           not  and   or    

           select * from user where  user_password not  like '%s%'

           select * from user where  user_id = 1 or user_id = 3  or user_id = 4 

         下划线表示一个字符,百分号表示多个字符
          查拥有字符%要在%前面加一个\,例:'%\%%'查询拥有%的记录   select * from user where  user_password like '%s%' 

                     DISTINCT   消除记录中重复的部分   select DISTINCT user_password from user   查询password记录,当有相同记录的时候显示一条

          ESCAPE  自定义一个转意字符        select * from user where  user_password like '%d%%' escape 'd'   其中d相当于\

          对特定数值进行查询    select * from user where  user_id in (1,3,4) 查id为134的记录


排序
          ORDER BY       排序   有两个条件时,先满足第一个条件     select * from user where  user_id not  in( 2,4) ORDER BY user_id  desc , user_sex asc     desc降序排列 asc升序排列     


大小写转换

            select user_id , LOWER(user_password) from user
            select user_id , UPPER(user_password) from user
       查询结果转换大小写 


分组函数
        group   by  select deptno ,avg(sal) from emp group by deptno   根据deptno分组来查询emp表中的 deptno和sal的平均值
      当需要附加条件时不用where,用having  
        select deptno ,avg(sal),max(sal),min(sal),sum(sal),count(empno) from emp group by deptno having avg(sal) >2000    根据deptno分组查询avg(sal)      >2000的记录


 子查询
        select *  from emp  where sal >(select avg(sal) from emp) order by sal    从emp中查询出所有 sal大于平均值的记录并排序
        select *  from (select * from emp)  e     e必须要
        子查询查出来结果的是一张表,即使只有一个数据也是一张表,临时表,不存在的表
        select *  from emp  where empno in (select empno from emp  where empno=7369 or empno=7499 or empno=7521) in与子查询的结合




连表语句
        select e.* ,d.dname from emp e,dept d where e.deptno=d.deptno 


        select e.* ,d.dname from emp e inner join  dept d on  e.deptno=d.deptno 
        inner join on 两边都有的才显示
        right join on  右边有,左边没有的也显示
        left join on    左边有,右边没有的也显示


         
分页查询
        select * from emp limit 0,5
        limit 1,2   第一个参数表示开始查询的行数的序列,第二个是查询记录的个数

原创粉丝点击