*Oracle ORA-00918:不明確的欄位定義 – SELECT 查詢問題**
對 Oracle 中的多個聯結表執行 SELECT *
查詢可能會導致可怕的 ORA-00918 錯誤:「列定義不明確」。當您的查詢包含來自不同表的具有相同名稱的列時,就會發生這種情況。 Oracle 無法確定您要檢索哪一列。
這是一個經常觸發此錯誤的範例查詢:
<code class="language-sql">SELECT * FROM (SELECT DISTINCT(coaches.id), people.*, users.*, coaches.* FROM "COACHES" INNER JOIN people ON people.id = coaches.person_id INNER JOIN users ON coaches.person_id = users.person_id LEFT OUTER JOIN organizations_users ON organizations_users.user_id = users.id ) WHERE rownum <= 25</code>
問題的根源
SELECT *
嘗試檢索所有列,但由於 id
存在於多個表中(coaches
、people
、users
),Oracle 不確定要選擇哪一個 id
列。
解:精確的列規範
要修復 ORA-00918,請避免 SELECT *
。 相反,明確列出您需要的列。 有兩種主要方法可以實現這一目標:
1。直接列選擇
使用其完全限定名稱 (table.column) 指定每列:
<code class="language-sql">SELECT coaches.id, people.name, users.username, coaches.team FROM (SELECT DISTINCT(coaches.id), people.*, users.*, coaches.* FROM "COACHES" INNER JOIN people ON people.id = coaches.person_id INNER JOIN users ON coaches.person_id = users.person_id LEFT OUTER JOIN organizations_users ON organizations_users.user_id = users.id ) WHERE rownum <= 25</code>
2。使用列別名
使用 關鍵字為同名的欄位指派唯一的別名:AS
<code class="language-sql">SELECT coaches.id AS coaches_id, people.name AS person_name, users.username AS user_username, coaches.team AS coaches_team FROM (SELECT DISTINCT(coaches.id), people.*, users.*, coaches.* FROM "COACHES" INNER JOIN people ON people.id = coaches.person_id INNER JOIN users ON coaches.person_id = users.person_id LEFT OUTER JOIN organizations_users ON organizations_users.user_id = users.id ) WHERE rownum <= 25</code>
以上是如何解決 Oracle 的 ORA-00918: SELECT * 查詢中的模糊列定義錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!