English 中文(简体)
加入两个表格,不返回不需要的行。
原标题:
  • 时间:2008-10-31 17:00:52
  •  标签:

我的表格结构看起来像这样:

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

有人能帮我到达想要的结果表吗?

最佳回答
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
问题回答

关于常识方面,我们辉煌的网站创始人写了一篇非常好的博客文章,我发现自己一遍又一遍地参考它。

Visual Explanation of SQL Joins

使用这个:

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

不确定括号是否需要。

这有效吗?

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.creatorid ORDER BY i.issueid

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





相关问题
热门标签