SELECT 一个数字是什么意思?

What does it mean to SELECT a number?

我在看我的书 SQL 反模式 中的一个例子。例子table是

         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

这应该return

t.ancestor  |   ???? 
---------------------
   5              ?
   5              ?
   5              ?

我以为

SELECT x, y

表示xy是名称或列,8不是列的名称。这到底是怎么回事?

在这种情况下,它们实际上只代表数字,这些是文字。由于您标记了 MySql,您可以检查它 here

所以子查询会 return

t.ancestor  |   8 
---------------------
   1              8
   4              8
   5              8

在账户中取UNION,会return

t.ancestor  |   8 
---------------------
   1              8
   4              8
   5              8
   8              8

在这种情况下,查询应该 return

t.ancestor   |   8
------------------
  1              8
  4              8
  5              8

你是 select t.ancestor 其中后代是 = 5

xy 表达式 ,它们对 table 中的每一行计算一次,其值被视为当前行中的相应列。 8也是一个表达式,每行计算一次,其值被取为常量“8”。

在这种情况下,查询将return

t.ancestor   |   8
------------------
  1              8
  4              8
  5              8

正如 scaisEdge 观察到的那样。