1052: Column 'id' in field list is ambiguous

前端 未结 8 1872
面向向阳花
面向向阳花 2020-11-22 13:17

I have 2 tables. tbl_names and tbl_section which has both the id field in them. How do I go about selecting the id field,

相关标签:
8条回答
  • 2020-11-22 13:58

    The simplest solution is a join with USING instead of ON. That way, the database "knows" that both id columns are actually the same, and won't nitpick on that:

    SELECT id, name, section
      FROM tbl_names
      JOIN tbl_section USING (id)
    

    If id is the only common column name in tbl_names and tbl_section, you can even use a NATURAL JOIN:

    SELECT id, name, section
      FROM tbl_names
      NATURAL JOIN tbl_section
    

    See also: https://dev.mysql.com/doc/refman/5.7/en/join.html

    0 讨论(0)
  • 2020-11-22 14:05

    If the format of the id's in the two table varies then you want to join them, as such you can select to use an id from one-main table, say if you have table_customes and table_orders, and tha id for orders is like "101","102"..."110", just use one for customers

    select customers.id, name, amount, date from customers.orders;
    
    0 讨论(0)
提交回复
热议问题