INNER JOIN same table
Asked Answered
S

6

37

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 row and user_parent_id row. I have coded something like this:

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

But it doesn't show the results. I want to display user record and its parent record.

Shamikashamma answered 10/2, 2013 at 10:24 Comment(1)
Maybe just SELECT * FROM users WHERE (id = 4 OR parent_id = 4) ?Prostatitis
V
51

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.

Veinstone answered 10/2, 2013 at 10:40 Comment(2)
Thanks everyone for your input.. I actually didnt have parent ID set while testing, now its working.. one more thing may be it will help someone, I was saving user_parents as TEXT in the format "1,2" so it means details of 2 users.. so in this scenario my final working query is below: $query = " select user.user_fname, user.user_lname, parent.user_lname AS plname, parent.user_fname AS pfname from {$prefix}users as user join {$prefix}users as parent on parent.user_id IN(user.user_parents) where user.user_id =".$id; thanks all once again..Shamikashamma
@Shamikashamma Never ever EVER store delimited data like that in columns.Tortola
R
2

You can also use UNION like

SELECT  user_fname ,
        user_lname
FROM    users 
WHERE   user_id = $_GET[id]
UNION
SELECT  user_fname ,
        user_lname
FROM    users 
WHERE   user_parent_id = $_GET[id]
Ricardaricardama answered 10/2, 2013 at 10:35 Comment(0)
P
1

Perhaps this should be the select (if I understand the question correctly)

select user.user_fname, user.user_lname, parent.user_fname, parent.user_lname
... As before
Polyanthus answered 10/2, 2013 at 10:26 Comment(0)
J
1

Your query should work fine, but you have to use the alias parent to show the values of the parent table like this:

select 
  CONCAT(user.user_fname, ' ', user.user_lname) AS 'User Name',
  CONCAT(parent.user_fname, ' ', parent.user_lname) AS 'Parent Name'
from users as user
inner join users as parent on parent.user_parent_id = user.user_id
where user.user_id = $_GET[id];
Jackquelinejackrabbit answered 10/2, 2013 at 10:26 Comment(0)
C
1

I don't know how the table is created but try this...

SELECT users1.user_id, users2.user_parent_id
FROM users AS users1
INNER JOIN users AS users2
ON users1.id = users2.id
WHERE users1.user_id = users2.user_parent_id
Counsellor answered 28/11, 2016 at 14:35 Comment(0)
K
1

Lets try to answer this question, with a good and simple scenario, with 3 MySQL tables i.e. datetable, colortable and jointable.

first see values of table datetable with primary key assigned to column dateid:

mysql> select * from datetable;
+--------+------------+
| dateid | datevalue  |
+--------+------------+
|    101 | 2015-01-01 |
|    102 | 2015-05-01 |
|    103 | 2016-01-01 |
+--------+------------+
3 rows in set (0.00 sec)

now move to our second table values colortable with primary key assigned to column colorid:

mysql> select * from colortable;
+---------+------------+
| colorid | colorvalue |
+---------+------------+
|      11 | blue       |
|      12 | yellow     |
+---------+------------+
2 rows in set (0.00 sec)

and our final third table jointable have no primary keys and values are:

mysql> select * from jointable;
+--------+---------+
| dateid | colorid |
+--------+---------+
|    101 |      11 |
|    102 |      12 |
|    101 |      12 |
+--------+---------+
3 rows in set (0.00 sec)

Now our condition is to find the dateid's, which have both color values blue and yellow.

So, our query is:

mysql> SELECT t1.dateid FROM jointable AS t1 INNER JOIN jointable t2
    -> ON t1.dateid = t2.dateid
    -> WHERE
    -> (t1.colorid IN (SELECT colorid FROM colortable WHERE colorvalue = 'blue'))
    -> AND
    -> (t2.colorid IN (SELECT colorid FROM colortable WHERE colorvalue = 'yellow'));
+--------+
| dateid |
+--------+
|    101 |
+--------+
1 row in set (0.00 sec)

Hope, this would help many one.

Kessel answered 19/4, 2018 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.