How do you avoid column name conflicts?

前端 未结 8 1160
挽巷
挽巷 2021-01-05 12:35

I was recently assigned a task of creating an auction system. During my work, I met numerous occasions where my SQL queries that contained joins failed to execute due to amb

8条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-05 13:20

    I've faced with MySQLSyntaxErrorException:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rank

    after executing the following query:

    insert into my_table (rank) values (1);

    Because rank column name clashes with sql keyword rank.

    SOLUTION:

    I've added back quotes to rank column name and problem was solved:

    insert into my_table (`rank`) values (1);

    Another way to solve the problem is to use table name instead of back quotes:

    insert into my_table (my_table.rank) values (1);

提交回复
热议问题