Avoiding ambiguous column errors when using JOIN in MySQL

前端 未结 2 1148
遥遥无期
遥遥无期 2021-01-29 05:00

My query looks like this:

$sql = \"
SELECT u.*, s.*
FROM bands u
inner join statuses s on u.status_id = s.id
WHERE u.status_id = 1
ORDER BY u.band_name\";
         


        
2条回答
  •  走了就别回头了
    2021-01-29 05:45

    You'll want to create non conflicting aliases for the id columns;

    SELECT u.*, s.*, u.id AS uid, s.id AS sid
    FROM bands u
    inner join statuses s on u.status_id = s.id
    WHERE u.status_id = 1
    ORDER BY u.band_name
    

    Then you can pick them out as $row['uid'] and $row['sid'] and still access your other columns as usual. The conflicting id column is also still there, just avoid using it.

提交回复
热议问题