Oracle 动态连接挑战赛

发布于 2024-08-07 06:37:44 字数 1157 浏览 3 评论 0原文

编辑:更简单的例子。 (原标题:Oracle 列注入)

目标: 完成下面的查询会生成以下结果吗?

用途:创建依赖于表中现有列的列,而不将表放入子查询中。

规则:

  1. 不能重构查询以将 tbl 放入子查询中。
  2. 查询必须使用a,b->1; x->2; y->3,而不是简单地连接到1,1,2,1,3
  3. 不得修改子查询tbl

 

SELECT val, cat
  FROM (SELECT 'a' val FROM DUAL  UNION ALL
        SELECT 'b' val FROM DUAL  UNION ALL
        SELECT 'x' val FROM DUAL  UNION ALL
        SELECT 'b' val FROM DUAL  UNION ALL
        SELECT 'y' val FROM DUAL) tbl
   ... JOIN ( ... ) ON ...

 val | cat
-----+-----
 'a' |  1
 'b' |  1
 'x' |  2
 'b' |  1
 'y' |  3

好吧,我已经做到了这一点(如下),但我无法添加第二个 LEFT JOIN。

SELECT val, cat
  FROM (SELECT 'a' val FROM DUAL  UNION ALL
        SELECT 'b' val FROM DUAL  UNION ALL
        SELECT 'x' val FROM DUAL  UNION ALL
        SELECT 'b' val FROM DUAL  UNION ALL
        SELECT 'y' val FROM DUAL) tbl
   LEFT JOIN ( SELECT 1 cat FROM DUAL ) ON val in ('a','b')

 val | cat
-----+-----
 'a' |  1
 'b' |  1
 'x' |  
 'b' |  1
 'y' |  

EDIT: Far simpler example. (Formerly titled: Oracle Column Injection)

GOAL: Complete the query below generate the following results?

PURPOSE: Create a column dependent on an existing column in a table without putting the table in a subquery.

Rules:

  1. Restructuring the query to put tbl in a subquery is not an option.
  2. The query must use a,b->1; x->2; y->3, not simply join to 1,1,2,1,3.
  3. The subquery tbl must not be modified.

 

SELECT val, cat
  FROM (SELECT 'a' val FROM DUAL  UNION ALL
        SELECT 'b' val FROM DUAL  UNION ALL
        SELECT 'x' val FROM DUAL  UNION ALL
        SELECT 'b' val FROM DUAL  UNION ALL
        SELECT 'y' val FROM DUAL) tbl
   ... JOIN ( ... ) ON ...

 val | cat
-----+-----
 'a' |  1
 'b' |  1
 'x' |  2
 'b' |  1
 'y' |  3

Well, I got this far (below), but I cannot add a second LEFT JOIN.

SELECT val, cat
  FROM (SELECT 'a' val FROM DUAL  UNION ALL
        SELECT 'b' val FROM DUAL  UNION ALL
        SELECT 'x' val FROM DUAL  UNION ALL
        SELECT 'b' val FROM DUAL  UNION ALL
        SELECT 'y' val FROM DUAL) tbl
   LEFT JOIN ( SELECT 1 cat FROM DUAL ) ON val in ('a','b')

 val | cat
-----+-----
 'a' |  1
 'b' |  1
 'x' |  
 'b' |  1
 'y' |  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扬花落满肩 2024-08-14 06:37:44
SELECT tbl.val, map.cat
  FROM (SELECT 'a' val FROM DUAL  UNION
        SELECT 'b' val FROM DUAL  UNION
        SELECT 'x' val FROM DUAL  UNION
        SELECT 'b' val FROM DUAL  UNION
        SELECT 'y' val FROM DUAL) tbl
   LEFT JOIN ( SELECT 'a' val, 1 cat FROM DUAL UNION
               SELECT 'b' val, 1 cat FROM DUAL UNION
               SELECT 'x' val, 2 cat FROM DUAL UNION
               SELECT 'y' val, 3 cat FROM DUAL ) map ON map.val = tbl.val

从您的示例中,我怀疑您的意思是使用 UNION ALL 而不是 UNION (以便在结果中出现 val = 'b' 的 2 行)。

您的“我到目前为止”示例甚至没有按照编写的方式运行 - 它需要 IN 列表周围的括号 - 即使您修复它也不会产生您显示的输出。

SELECT tbl.val, map.cat
  FROM (SELECT 'a' val FROM DUAL  UNION
        SELECT 'b' val FROM DUAL  UNION
        SELECT 'x' val FROM DUAL  UNION
        SELECT 'b' val FROM DUAL  UNION
        SELECT 'y' val FROM DUAL) tbl
   LEFT JOIN ( SELECT 'a' val, 1 cat FROM DUAL UNION
               SELECT 'b' val, 1 cat FROM DUAL UNION
               SELECT 'x' val, 2 cat FROM DUAL UNION
               SELECT 'y' val, 3 cat FROM DUAL ) map ON map.val = tbl.val

From your examples, I suspect you mean to use UNION ALL instead of UNION (in order to have 2 rows for val = 'b' appear in the result).

Your "I got this far" example doesn't even run as written -- it needs parentheses around the IN list -- and even once you fix that it doesn't produce the output you show.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文