Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如果子集组合返回空结果,是否查询获取超集?_Sql_Oracle_Postgresql - Fatal编程技术网

Sql 如果子集组合返回空结果,是否查询获取超集?

Sql 如果子集组合返回空结果,是否查询获取超集?,sql,oracle,postgresql,Sql,Oracle,Postgresql,需要根据输入返回vacId,如果我输入,如果代码+类型组合不存在,则只返回等于给定类型的vacId TYPE=A,CODE=BIZ,GENDER=M,那么它应该返回110 TYPE=A和CODE=SYS则应返回110和113,114没有A+SYS组合,因此返回超集A的数据 我如何为这个问题编写一个查询,目前应该可以在pgsql和oracle中使用 有没有一种方法可以通过使用合并实现这一点?您可以使用条件where子句,例如($用于参数): 更新 另一种解决方案是在单个json对象中获得结果,这将

需要根据输入返回vacId,如果我输入,如果代码+类型组合不存在,则只返回等于给定类型的vacId

  • TYPE=A,CODE=BIZ,GENDER=M,那么它应该返回110
  • TYPE=A和CODE=SYS则应返回110和113,114没有A+SYS组合,因此返回超集A的数据
  • 我如何为这个问题编写一个查询,目前应该可以在pgsql和oracle中使用


    有没有一种方法可以通过使用合并

    实现这一点?您可以使用条件
    where
    子句,例如(
    $
    用于参数):

    更新

    另一种解决方案是在单个json对象中获得结果,这将使您能够使用
    coalesce()


    这是否会多次影响数据库?此查询中有任何性能问题吗?可以在此处添加多个事例吗?子查询涉及额外的扫描,但比主查询便宜,尤其是在使用适当的索引时。您可以在
    case
    语句中使用多个子查询,只需在。。。然后…在
    else
    之前。只是一个额外的查询。为了降低性能风险,是否最好返回所有json数据并在视图(前端)上进行过滤?这在很大程度上取决于查询返回的实际数据。基本上,您应该减少从服务器传输的数据量。使用合适的索引,带有条件
    WHERE
    子句的解决方案可能非常有效,并且比在客户端过滤数据要好。替代解决方案-定义了WHERE tojsonb()函数?
    TABLE : **PROFILE**
    RUN_ID| Type | code | gender | vacId
    
    1     | A    | biz  | M      | 110
    2     | A    | pro  | M      | 113
    3     | A    | tot  | F      | 114
    
    select p.*
    from profile p
    where case
        when exists (
            select 1
            from profile
            where type = $type and code = $code and gender = $gender
            )
        then type = $type and code = $code and gender = $gender
        else type = $type
        end;
    
    select coalesce(
        (select jsonb_agg(to_jsonb(p))
        from profile p
        where type = $type and code = $code and gender = $gender),
        (select jsonb_agg(to_jsonb(p))
        from profile p
        where type = $type)
    );