1

我有以下 SQL 语句

SELECT be.*, it.order_number
FROM songs AS be

INNER JOIN
(
    SELECT song_id, order_number
    FROM items
    WHERE order_status = 1
) it
ON be.id = it.song_id

INNER JOIN orders AS or
ON it.order_number = or.order_number

WHERE be.active = 0

我似乎无法理解为什么这个语句没有产生任何结果。当我删除以下行时;

INNER JOIN orders AS or
ON it.order_number = or.order_number

它似乎产生了结果,我知道表中order_number确实存在orders- 所以它显然是不正确的语法,但我不知道从哪里开始?感谢帮助。

4

1 回答 1

1

这个特定实例中的问题是or查询中的 是保留字。如果你愿意,你可以使用它,但你必须引用它,就像这样

SELECT be.*, it.order_number
FROM songs AS be

INNER JOIN
(
    SELECT song_id, order_number
    FROM items
    WHERE order_status = 1
) it
ON be.id = it.song_id

INNER JOIN orders AS "or"
ON it.order_number = "or".order_number

WHERE be.active = 0

不过,一般来说,为了可读性,我会避免使用这样的名称。如果你必须引用它或转义它,它可能是一个坏名字。

于 2012-09-24T22:47:28.220 回答