Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INNER JOIN same table

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.

like image 828
user1718343 Avatar asked Feb 10 '13 10:02

user1718343


People also ask

Can you inner join on the same table?

SQL Server self join syntax It helps query hierarchical data or compare rows within the same table. A self join uses the inner join or left join clause. Because the query that uses the self join references the same table, the table alias is used to assign different names to the same table within the query.

Can you join on the same table twice?

As you may know, it is used to join and combine data from two or more tables into one common data set. In this article, I'm going to discuss special types of joins? in which you combine the same table twice—including joining a table to itself, also known as the self join.

Can I join same table in SQL?

The self-join is a special kind of joins that allow you to join a table to itself using either LEFT JOIN or INNER JOIN clause. You use self-join to create a result set that joins the rows with the other rows within the same table.

What join is used to combine the same table itself?

A self join is a regular join, but the table is joined with itself.


3 Answers

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.

like image 88
Ronnis Avatar answered Sep 30 '22 17:09

Ronnis


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]
like image 43
Prashant16 Avatar answered Sep 30 '22 18:09

Prashant16


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.

like image 2
ArifMustafa Avatar answered Sep 30 '22 18:09

ArifMustafa