INNER JOIN same table

后端 未结 6 794
盖世英雄少女心
盖世英雄少女心 2021-01-01 11:32

I am trying to get some rows from the same table. It\'s a user table: user has user_id and user_parent_id.

I need to get the user_id

6条回答
  •  臣服心动
    2021-01-01 12:18

    I think the problem is in your JOIN condition.

    SELECT user.user_fname,
           user.user_lname,
           parent.user_fname,
           parent.user_lname
    FROM users AS user
    JOIN users AS parent 
      ON parent.user_id = user.user_parent_id
    WHERE user.user_id = $_GET[id]
    

    Edit: You should probably use LEFT JOIN if there are users with no parents.

提交回复
热议问题