选择一个数字是什么意思?

时间:2016-01-02 16:08:42

标签: mysql sql algorithm database-design tree

我正在查看我的书中的示例 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

表示xy是名称或列,而8不是列的名称。到底发生了什么事?

3 个答案:

答案 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)

xy表达式,对表中的每一行计算一次,其值将作为当前行中的相应列。 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

相关问题