1

我试图在查询的一行中选择两个用户名,一个称为“用户名”,第二个说“用户名 2”。这是我有atm的代码。users 表使用 user_id 的主键。

SELECT
  r.report_id,
  r.poster_id,
  r.reporter_id,
  u.username,
  (2nd username)
FROM reports r,
  users u
WHERE r.report_id = :report_id
    AND r.reporter_id = u.user_id
    AND r.poster_id = u.user_id
4

2 回答 2

2

你需要加入表user两次

SELECT 
        r.report_id,
        r.poster_id,
        r.reporter_id,
        u.username AS ReporterName,
        b.userName as PosterName
FROM
        reports r
        INNER JOIN users u      
            ON r.reporter_id=u.user_id
        INNER JOIN users b
            ON r.poster_id=b.user_id
WHERE
        r.report_id=:report_id
于 2013-01-21T08:20:28.243 回答
1

这是MySQL的代码

        SELECT
          r.report_id,
          r.poster_id,
          r.reporter_id,
          u.username,
          u.username username2,
        FROM reports r,
          users u
        WHERE r.report_id = :report_id
            AND r.reporter_id = u.user_id
            AND r.poster_id = u.user_id
于 2013-01-21T08:26:11.833 回答