MySQL INNER JOIN 同一个表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14796738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:29:43  来源:igfitidea点击:

INNER JOIN same table

mysqlsqljoinself-join

提问by user1718343

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

我试图从同一个表中获取一些行。这是一个用户表:用户有user_iduser_parent_id

I need to get the user_idrow and user_parent_idrow. I have coded something like this:

我需要得到user_id行和user_parent_id行。我已经编码了这样的东西:

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.

但它没有显示结果。我想显示用户记录及其父记录。

回答by Ronnis

I think the problem is in your JOINcondition.

我认为问题出在你的JOIN条件上。

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 JOINif there are users with no parents.

编辑LEFT JOIN如果有没有父母的用户,您可能应该使用。

回答by Prashant16

You can also use UNION like

您也可以使用 UNION 之类的

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]

回答by Ed Heal

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

回答by Mahmoud Gamal

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

您的查询应该可以正常工作,但您必须使用别名parent来显示父表的值,如下所示:

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];

回答by ioses

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

回答by ArifMustafa

Lets try to answer this question, with a good and simple scenario, with 3 MySQLtables i.e. datetable, colortableand jointable.

让我们尝试用一个简单而简单的场景来回答这个问题,有 3 个MySQL表,即datetablecolortablejoinable

firstsee values of tabledatetablewith primary keyassigned to columndateid:

首先看到的值tabledatetableprimary key分配给columndateid

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 secondtablevalues colortablewith primary keyassigned to columncolorid:

现在转到我们的第二个tablecolortableprimary key分配给columncolorid

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

and our final thirdtablejointablehave noprimary keysand values are:

而我们最终的第三tablejointable没有primary keys和价值观是:

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

Nowour condition is to find the dateid's, which have both colorvalues blueand yellow.

现在我们的条件是找到dateid 的,它具有颜色blueYellow

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.

希望,这会帮助很多人。