How to fix an “ambigous column name error using inner join” error

后端 未结 3 650
深忆病人
深忆病人 2021-01-25 06:56

I am taking a query from a database, using two tables and am getting the error described in the title of my question. In some cases, the field I need to query by is in table A,

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-25 07:19

    You have to write your where clause in such a way that you can say A.field_from_A or B.field_from_B. You can always pass A.field_from_A.

    Although, you don't really want to say

    SELECT * FROM A INNER JOIN B ON A.id=B.id where B.id = '1'.

    You would want to say

    SELECT * FROM B INNER JOIN A ON B.id=A.id where B.id = '1'

    You can get some really slow queries if you try to use a joined table in the where clause. There are times when it's unavoidable, but best practice is to always have your where clause only call from the main table.

提交回复
热议问题