Select

‘ambiguous column name’ 錯誤指的是數據庫、表和列 guuu

  • March 26, 2019

sqlite3 在下面的查詢中給出了“不明確的列名”錯誤。錯誤本身引用了一個可以想像的明確的列名,將數據庫標識為“sourcedb”,將表標識為“answers”,將欄位標識為“markup”。為什麼這個查詢會觸發錯誤?

sqlite> SELECT answers.*
       FROM   sourcedb.answers
              INNER JOIN sourcedb.questions_tests
                ON sourcedb.questions_tests.testskey = '121212eczema'
              INNER JOIN sourcedb.answers_questions
                ON sourcedb.questions_tests.questionskey = sourcedb.answers.questionskey
              INNER JOIN sourcedb.answers
                ON sourcedb.answers.answerskey = sourcedb.answers_questions.questionskey; 
...>    ...>    ...> Error: ambiguous column name: sourcedb.answers.markup

一些額外的細節…

sqlite> pragma table_info(answers);
0|markup|TEXT|0||0
1|identifier|TEXT|0||0
2|text|TEXT|0||0
3|answerskey|INTEGER|0||1
4|answertype|TEXT|0||0
5|ReadPerms||0||0
6|UpdatePerms||0||0
7|DeletePerms||0||0
sqlite> pragma database_list;
0|main|
2|sourcedb|/root/.ttest/database/sqlite/ttest-simple.sqlite

你有FROM sourcedb.answersINNER JOIN sourcedb.answers所以你在沒有給出別名的情況下兩次引入同一個表。

一些連接條件也可能出現錯誤。

FROM   sourcedb.answers
              INNER JOIN sourcedb.questions_tests
                ON sourcedb.questions_tests.testskey = '121212eczema'

sourcedb.answers引用ON.

INNER JOIN sourcedb.answers_questions
                ON sourcedb.questions_tests.questionskey = sourcedb.answers.questionskey

sourcedb.answers_questions中沒有引用ON

我想你實際上需要類似的東西

SELECT answers.*
FROM   sourcedb.answers
      INNER JOIN sourcedb.answers_questions
        ON sourcedb.answers.answerskey = sourcedb.answers_questions.questionskey
      INNER JOIN sourcedb.questions_tests
        ON sourcedb.questions_tests.questionskey = sourcedb.answers.questionskey
WHERE  sourcedb.questions_tests.testskey = '121212eczema' 

引用自:https://dba.stackexchange.com/questions/46717