我正在查看我的书中的示例 SQL Antipatterns 。示例表是
TreePaths
==============================
ancestor | descendant
------------------------------
1 1
1 2
1 3
1 4
1 5
1 6
1 7
2 2
2 3
3 3
4 4
4 5
4 6
4 7
5 5
6 6
6 7
7 7
并询问我对此感到困惑
INSERT INTO TreePaths (ancestor, descendant)
SELECT t.ancestor, 8
FROM TreePaths AS t
WHERE t.descendant = 5
UNION ALL
SELECT 8,8
因此,让我首先介绍子查询
SELECT t.ancestor, 8
FROM TreePaths AS t
WHERE t.descendant = 5
这应该返回
t.ancestor | ????
---------------------
5 ?
5 ?
5 ?
我想
SELECT x, y
表示x
和y
是名称或列,而8
不是列的名称。到底发生了什么事?
答案 0 :(得分:2)
在这种情况下,它们实际上只代表数字,这些是文字。由于您标记了MySql,因此可以对其进行检查here。
因此子查询将返回
t.ancestor | 8
---------------------
1 8
4 8
5 8
将UNION纳入帐户,它将返回
t.ancestor | 8
---------------------
1 8
4 8
5 8
8 8
答案 1 :(得分:1)
x
和y
是表达式,对表中的每一行计算一次,其值将作为当前行中的相应列。 8
也是一个表达式,为每一行计算一次,其值被视为常量“8”。
在这种情况下,查询将返回
t.ancestor | 8
------------------
1 8
4 8
5 8
scaisEdge观察到了。
答案 2 :(得分:0)
在这种情况下,查询应该返回
t.ancestor | 8
------------------
1 8
4 8
5 8
你是选择t.ancestor,其中后代是= 5