Oracle 动态连接挑战赛
编辑:更简单的例子。 (原标题:Oracle 列注入)
目标: 完成下面的查询会生成以下结果吗?
用途:创建依赖于表中现有列的列,而不将表放入子查询中。
规则:
- 不能重构查询以将
tbl
放入子查询中。 - 查询必须使用
a,b->1; x->2; y->3
,而不是简单地连接到1,1,2,1,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:
- Restructuring the query to put
tbl
in a subquery is not an option. - The query must use
a,b->1; x->2; y->3
, not simply join to1,1,2,1,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的示例中,我怀疑您的意思是使用 UNION ALL 而不是 UNION (以便在结果中出现 val = 'b' 的 2 行)。
您的“我到目前为止”示例甚至没有按照编写的方式运行 - 它需要 IN 列表周围的括号 - 即使您修复它也不会产生您显示的输出。
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.