2

我的表结构如下所示:

      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  |
+---------+------------+-----------+-----------+

谁能帮我找到想要的结果表?

4

6 回答 6

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
于 2008-10-31T17:07:04.973 回答
3

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

SQL 连接的可视化解释

于 2008-10-31T17:29:21.557 回答
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
于 2008-10-31T17:07:05.753 回答
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

不确定是否需要括号。

于 2008-10-31T17:06:38.907 回答
0

这行得通吗?

SELECT i.issueid, i.assignedid, u1.real_name as assigned_name, i.creatorid, u2.real_name as creator_name FROM users u1 INNER JOIN issues i ON u1.userid = i.assignedid INNER JOIN users u2 ON u2.userid = i.创建者 ID ORDER BY i.issueid

于 2008-10-31T17:10:27.977 回答
0

SELECT
i.issueid,
i.assignedid,
a.real_name,
i.creatorid,
c.real_name
FROM
issues i
INNER JOIN users c
ON c.userid = i.creatorid
INNER JOIN users a
ON a.userid = i.assignedid
ORDER BY
i .issueid ASC

于 2008-10-31T17:26:31.253 回答