我一直得到错误代码1248每个派生表必须有自己的别名

时间:2014-05-05 04:06:23

标签: mysql

我不断收到错误代码1248每个派生表必须在下面有自己的别名,并且无法弄清楚原因。我已经尝试过几次重写,看看我是否能抓住我犯错的地方,但我想我忽略了一些东西。

SELECT order_id, order_date, c.customer_id, last_name
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id,
(
    SELECT phone
    FROM addresses a
    JOIN orders od ON a.customer_id = od.customer_id
)
WHERE address_id = billing_address_id AND ship_date IS NULL;

2 个答案:

答案 0 :(得分:0)

我不确定你要做的是什么,但我认为你的问题是你没有给你的子查询赋予别名,这在JOIN中是必需的

SELECT order_id, order_date, c.customer_id, last_name
FROM customers c
    JOIN orders o ON c.customer_id = o.customer_id,
(SELECT phone
FROM addresses a
    JOIN orders od ON a.customer_id = od.customer_id) as subTable
WHERE address_id = billing_address_id AND ship_date IS NULL;

答案 1 :(得分:0)

猜猜你在找这样的东西(不知道我是否把所有的田地都弄好了,但要确保你仔细检查;下次请给桌子结构):

SELECT
o.`order_id`,
o.`order_date`,
o.`customer_id`,
c.`last_name`,
a.`phone`
FROM
orders AS o 
LEFT JOIN customers AS c ON o.customer_id = c.customer_id
LEFT JOIN addresses AS a ON (
    o.customer_id = a.customer_id
    AND a.address_id = o.billing_address_id
)
WHERE
ship_date IS NULL;