连接两个表而不返回不需要的行

时间:2008-10-31 17:00:53

标签: mysql join

我的表格结构如下:

      tbl.users                       tbl.issues
+--------+-----------+   +---------+------------+-----------+
| userid | real_name |   | issueid | assignedid | creatorid |
+--------+-----------+   +---------+------------+-----------+
|   1    |   test_1  |   |    1    |     1      |     1     |
|   2    |   test_2  |   |    2    |     1      |     2     |
+--------+-----------+   +---------+------------+-----------+

基本上我想写一个查询,它将在结果表中结束,如下所示:

                           (results table)
+---------+------------+---------------+-----------+--------------+
| issueid | assignedid | assigned_name | creatorid | creator_name |
+---------+------------+---------------+-----------+--------------+
|    1    |     1      |    test_1     |    1      |    test_1    |
|    2    |     1      |    test_1     |    2      |    test_2    |
+---------+------------+---------------+-----------+--------------+

我的SQL目前看起来像这样:

SELECT 
  `issues`.`issueid`,
  `issues`.`creatorid`,
  `issues`.`assignedid`,
  `users`.`real_name`
FROM `issues`
JOIN `users` 
  ON ( `users`.`userid` = `issues`.`creatorid` )
  OR (`users`.`userid` = `issues`.`assignedid`)
ORDER BY `issueid` ASC
LIMIT 0 , 30 

返回如下内容:

                (results table)
+---------+------------+-----------+-----------+
| issueid | assignedid | creatorid | real_name |
+---------+------------+-----------+-----------+
|    1    |     1      |     1     |   test_1  |
|    2    |     1      |     2     |   test_1  |
|    2    |     1      |     2     |   test_2  |
+---------+------------+-----------+-----------+

有人可以帮我找到理想的结果表吗?

6 个答案:

答案 0 :(得分:7)

SELECT 
  IssueID, 
  AssignedID, 
  CreatorID, 
  AssignedUser.real_name AS AssignedName, 
  CreatorUser.real_name AS CreatorName
FROM Issues
  LEFT JOIN Users AS AssignedUser
         ON Issues.AssignedID = AssignedUser.UserID
  LEFT JOIN Users AS CreatorUser
         ON Issues.CreatorID = CreatorUser.UserID
ORDER BY `issueid` ASC
LIMIT 0, 30

答案 1 :(得分:3)

在一般知识方面,我们杰出的网站创始人写了一篇关于这个主题的非常好的博客文章,我发现自己一遍又一遍地提到。

Visual Explanation of SQL Joins

答案 2 :(得分:1)

使用此:

SELECT 
`issues`.`issueid`,
`issues`.`creatorid`,
`creator`.`real_name`,
`issues`.`assignedid`,
`assigned`.`real_name`
FROM `issues` i
INNER JOIN `users` creator ON ( `creator`.`userid` = `issues`.`creatorid` )
INNER JOIN `users` assigned ON (`assigned`.`userid` = `issues`.`assignedid`)
ORDER BY `issueid` ASC
LIMIT 0 , 30

答案 3 :(得分:0)

SELECT DISTINCT (i.issueid, i.creatorid, i.assignedid, u.real_name)
FROM issues i, users u
WHERE u.userid = i.creatorid OR u.userid = assignedid
ORDER BY i.issueid ASC
LIMIT 0 , 30

不确定是否需要括号。

答案 4 :(得分:0)

这有用吗?

选择 i.issueid, i.assignedid, u1.real_name as assigned_name, i.creatorid, u2.real_name as creator_name 来自用户u1 INNER JOIN问题我在u1.userid = i.assignedid上 INNER JOIN用户u2 ON u2.userid = i.creatorid 订单来自i.issueid

答案 5 :(得分:0)

选择
i.issueid,
i.assignedid,
a.real_name,
i.creatorid,
c.real_name

问题我是 INNER JOIN用户c
ON c.userid = i.creatorid
INNER JOIN用户a ON a.userid = i.assignedid
订购单 i.issueid ASC