如this線程中所述,from子句中的子查詢不是必需的,並且會產生笛卡爾積,但會出現新問題。JOIN別名不存在並且不正確地結束
SELECT customer.customer_name
,orders.order_date
,order_line.num_ordered
,order_line.quoted_price
,part.descript
,amt_billed
FROM (SELECT order_line.num_ordered*part.price AS amt_billed
FROM order_line
JOIN part
ON order_line.part_num = part.part_num
) billed
,customer
JOIN orders
ON customer.customer_num = orders.customer_num
JOIN order_line
ON orders.order_num = order_line.order_num
JOIN part
ON order_line.part_num = part.part_num;
讓我們說我決定在from子句中使用子查詢。如果您閱讀了註釋,@JorgeCampos建議我使用AS
來建立別名,然後在子查詢和現有表之間編寫連接。
當我在子查詢和別名billed
之間放置AS
時,我得到一個ORA-00933: SQL command not properly ended
。刪除AS
並運行它不會返回錯誤,而是笛卡爾產品。
此外,我嘗試使用billed
別名返回ORA-00942: table or view does not exist
。
最後,我想知道如何,如果我下定決心要保持子查詢在FROM子句中,這將被寫入。
我正在使用Oracle 11g pl/sql。
更新
此:
SELECT customer.customer_name
,orders.order_date
,order_line.num_ordered
,order_line.quoted_price
,part.descript
,amt_billed
FROM (SELECT order_line.num_ordered*part.price AS amt_billed, orders.order_num
FROM order_line
JOIN part
ON order_line.part_num = part.part_num
) AS billed
JOIN customer
ON customer.customer_num = orders.customer_num
JOIN billed
ON orders.order_num = order_line.order_num
JOIN orders
ON customer.customer_num = orders.customer_num
JOIN order_line
ON orders.order_num = order_line.order_num
JOIN part
ON order_line.part_num = part.part_num;
返回此:
) AS billed
*
ERROR at line 11:
ORA-00933: SQL command not properly ended
在行刪除AS
11返回此:
JOIN billed
*
ERROR at line 14:
ORA-00942: table or view does not exist
更新
感謝@GordonLinoff,我呈現給你的最終代碼!
SELECT customer.customer_name
,orders.order_date
,order_line.num_ordered
,order_line.quoted_price
,amt_billed
,part.descript
FROM customer
JOIN orders
ON customer.customer_num = orders.customer_num
JOIN order_line
ON orders.order_num = order_line.order_num
JOIN part
ON order_line.part_num = part.part_num
JOIN (SELECT DISTINCT order_line.part_num
,order_line.num_ordered*part.price AS amt_billed
FROM order_line
JOIN part
ON order_line.part_num = part.part_num
) billed
ON billed.part_num = order_line.part_num
ORDER BY customer.customer_name, orders.order_date;
我照他說的去做了,並且刪除了給客戶的冗餘連接。我也不得不將這些子選擇加入進來。如果我堅持num_ordered
,那麼只要訂購了一個零件,行就會重複它們自己。
我明白了。是否正在創建一個'JOIN'來引用在'FROM'子句中聲明的別名,Oracle也不支持? –
@ConnerSams。 。 。我不明白評論中的問題。對於定義表別名,Oracle不承認「as」。當然,一旦定義了表別名,它可以用於後續的'on'子句以及'where','group by','having','select','order by'。 –
我說的是這樣的:撤除在第11個返回此: JOIN計費 * 行出現錯誤14: ORA-00942:表或視圖不存在 SQL甲骨文 –