SQL Cookbook阅读记录

本文介绍了SQL中的多种实用查询技巧,包括使用条件、限制查询结果数量、连接列值、处理空值等,适用于多种数据库系统如MySQL、Oracle、SQL Server等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值