MySQL 'user_id' in where clause is ambiguous problem

后端 未结 4 1638
逝去的感伤
逝去的感伤 2020-11-28 16:49

How can I correct the problem I keep getting from the code below which states \'user_id\' in where clause is ambiguous. Thanks for the help in advance.

相关标签:
4条回答
  • 2020-11-28 16:50

    You should be mention alias name of your column and assign to user_id like

    SELECT user.*, user_info.* 
    FROM user as u 
    INNER JOIN user_info as ui ON u.user_id = ui.user_id
    WHERE u.user_id='$user_id'
    
    0 讨论(0)
  • 2020-11-28 16:57

    You simply need to specify which user_id to use, since both the user_info and user table have a field called user_id:

    ... WHERE user.user_id='$user_id'
    

    SQL wouldn't tolerate this ambiguity, since for what it knows, both fields can represent totally different data.

    0 讨论(0)
  • 2020-11-28 16:58

    The solution is to select each column explicitly everywhere. Don't use user.* and user_info.* in your select list, either:

    SELECT u.user_id, u.user_fudge, ui.foo, ui.bar
    FROM user u
    INNER JOIN user_info ui
        ON ui.user_id = u.user_id
    WHERE u.user_id = '$user_id';
    
    0 讨论(0)
  • 2020-11-28 17:10

    Try this.

    SELECT u.user_id, u.user_fudge, ui.foo, ui.bar
    FROM user AS u
    INNER JOIN user_info AS ui
       ON ui.user_id = u.user_id
    WHERE u.user_id = '$user_id';
    
    0 讨论(0)
提交回复
热议问题