-3

I keep getting error code 1248 every derived table must have its own alias on the following and cannot figure out why. I have tried rewriting several times to see if I can catch where I am making a mistake but I guess I am overlooking something.

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;
Phil
  • 157,677
  • 23
  • 242
  • 245

2 Answers2

0

I am not sure what you are trying to do is, but I think your problem is you haven't given aliases to your subquery ,which is required in 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;
AK47
  • 3,707
  • 3
  • 17
  • 36
0

Guess your looking for something like this (don't know if I got all the fields right, but make sure you double check; next time give table structures please) :

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;
Tanatos
  • 1,857
  • 1
  • 13
  • 12