YoloNaV
V2EX  ›  数据库

请教一个 oracle 的排序问题

  •  
  •   YoloNaV · Jan 18, 2022 · 2462 views
    This topic created in 1772 days ago, the information mentioned may be changed or developed.

    目前有一个场景是

    条件 A 执行 select * from table order by a, b c ,d

    条件 B 执行 select * from table order by b, c, a,d

    想请问下 oracle 大佬,oracle 数据能不能实现把这两个语句写成一个语句, 网上查了 case when 的功能和我想要的是完全不是一个东西。

    19 replies  •  2022-01-20 09:39:43 +08:00
    YoloNaV
    YoloNaV
    OP
    Jan 18, 2022
    1
    就类似这样的
    select * from table
    if A then order by a, b c ,d
    if B then order by b, c, a,d
    yinzhili
    yinzhili
    Jan 18, 2022
    2
    是不是要根据表字段值做逻辑判断,类似这样:
    select * from table t
    order by case when t.id>10000 then t.id else t.name end desc
    YoloNaV
    YoloNaV
    OP
    Jan 18, 2022
    3
    @yinzhili 这个 case when 满足不了这个功能

    其实就是外部有个参数 A 为 1 或者 0 ,
    A 如果是 0 ,则执行

    select * from table order by a, b c ,d

    A 如果是 1 ,则执行

    select * from table order by b, c, a,d ,
    想在是想把这两句 sql 合成一个 sql
    RinHoshizora
    RinHoshizora
    Jan 18, 2022❤️2
    4
    select *
    from table
    order by CASE WHEN A = 0 THEN a END,
    b, c,
    CASE WHEN A = 1 THEN a END,
    d
    Tenlearn
    Tenlearn
    Jan 18, 2022
    5
    直接代码多好,这实现不了,性能上也不能这么用
    shyrock
    shyrock
    Jan 18, 2022
    6
    用 union 把两条语句的结果集合并了。
    YoloNaV
    YoloNaV
    OP
    Jan 18, 2022 via iPhone
    7
    @RinHoshizora 感谢
    YoloNaV
    YoloNaV
    OP
    Jan 18, 2022 via iPhone
    8
    @shyrock 谢谢,是两个不同的结果,不能 union 哦
    YoloNaV
    YoloNaV
    OP
    Jan 18, 2022 via iPhone
    9
    @Tenlearn 就是不能用代码写我才要这样实现的
    shyrock
    shyrock
    Jan 18, 2022
    10
    @zww340277220 #8 两个数据集的字段是一样的,一个为空,一个有数据,应该可以 union
    c6h6benzene
    c6h6benzene
    Jan 18, 2022
    11
    [select A] where :condition = A union [select B] where :condition = B
    c6h6benzene
    c6h6benzene
    Jan 18, 2022
    12
    啊,正常的话要 union 之后才能 order by😅
    c6h6benzene
    c6h6benzene
    Jan 18, 2022
    13
    YoloNaV
    YoloNaV
    OP
    Jan 19, 2022
    14
    @shyrock 数据集一样,数据也全部一样,但是排列顺序不一样,union 后不就有两份数据了嘛?还是不能用 union 的,
    YoloNaV
    YoloNaV
    OP
    Jan 19, 2022
    15
    @c6h6benzene 感谢,大佬提示,再去试试 decode 能不能行。
    yinzhili
    yinzhili
    Jan 19, 2022
    16
    @zww340277220 根据外部入参做判断?如果是 mybatis 就有 choose when otherwise 可以用
    shyrock
    shyrock
    Jan 19, 2022
    17
    @zww340277220 #14 被你绕晕了,你 A 和 B 两个条件难道不是互斥的?存在 A 和 B 都满足的情况?
    YoloNaV
    YoloNaV
    OP
    Jan 20, 2022
    18
    @shyrock 不存在啊,就是两个 order 的排序顺序是不同的,根据外部条件 1 或者 0 来选择哪个排序条件,因为代码中写 sql 语句是用的 oracle 的 C API 接口写的,sql 语句都是写在注释里面,类似这种格式

    //{{COMPATIBILITY(KGDB_ORACLE)
    /*
    EXEC SQL DECLARE MY_CURSOR CURSOR FOR
    SELECT * FROM TABLE_NAME ORDER BY A, B, C, D;
    */
    //}}COMPATIBILITY

    所以我才会提这个问题的。
    YoloNaV
    YoloNaV
    OP
    Jan 20, 2022
    19
    @yinzhili
    @zww340277220 感谢,确实可以,不过就是感觉怪怪的,别人维护起来可能看不懂什么意思。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4271 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 09:12 · PVG 17:12 · LAX 01:12 · JFK 04:12
    ♥ Do have faith in what you're doing.