SQL Cookbook阅读记录

SQl CookBook记录

记录

  • 索行和列
    • 部分行
    • 件行
    • SELECT中使用

      case when ...

          chen ...

          else ...

      end

       

      • case
    • 返回限制的行

      DB2-----------------------select * from emp fetch first 5 rows only

      MySQL PostgreSQL ---------select * from emp limit 5

      Oracle -------------------select * from emp where rownum <=5(ROWNUM是在获取每行之后才赋予的)

      SQL Server----------------select top 5 * from emp

       

  • 机返回n条记录

    DB2 --------------------- select * from emp order by rand() fetch first 5 rows only

    MySQL-------------------- select * from emp order by rand() limit 5

    PostgreSQL--------------- select * from emp order by random() limit 5

    Oracle------------------- select * from (select * from emp order by dbms_random.value()) where rownum <= 5

    SQL Server--------------- select top 5 * from emp order by newid()

     

    • order by
  • 按模式搜索

    select * from emp where deptno in (10,20)

    select * from emp where ename like '%I%' or job like '%ER'

     

    • in
    • like
  • 索列
    • 部分列
    • 列取名
    • WHERE中引用取名的列

      将查询作为内联视图

      select * from

          (select sal as salary, comm as commission from emp) x

      where salary < 5000

       

      • 内联视图
    • 接列

      DB2,Oracle, PostgreSQL --- ||

      MySQL -------------------- concat()

      SQL server --------------- +

       

    • 找空

      select * from emp where comm is null

       

      • IS NULL
    • 转换

      select coalesce(comm,0) from emp

      select case

          when comm is null then 0

          else comm

          end

      from emp

       

      • COALESCE()
      • CASE

猜你喜欢

转载自blog.csdn.net/yeeman/article/details/6366705