1

问题:

我正在尝试将评分表(betyg_essays)中两列中的id“翻译”为另一个表(betyg_users)中这些用户的真实姓名。

评分表 (betyg_essays) 如下所示:

在此处输入图像描述

用户表 (betyg_users) 如下所示:

在此处输入图像描述

PHP代码(到目前为止):

$query = 'SELECT * FROM betyg_essays JOIN betyg_users ON betyg_essays.Examiner = betyg_users.UID';

检查员/主管列 (betyg_essays) 对应于 UID 列 (betyg_users)。

所需的输出应该是(使用名字/姓氏列):

  1. 考官:约翰霍普金斯。主管:迈克·贝克。
  2. 考官:约翰霍普金斯。主管:迈克·贝克。

非工作 SQL 查询:

$query = "SELECT 
            (u1.Firstname + ' ' + u1.Lastname) AS Examiner, 
            (u2.Firstname + ' ' + u2.Lastname) AS Supervisor
        FROM betyg_essays grade
            INNER JOIN betyg_users u1 ON grade.Examiner = u1.UID
            INNER JOIN betyg_users u2 ON grade.Supervisor = u2.UID";
4

4 回答 4

2
SELECT
CONCAT(u1.FirstName, ' ', u1.LastName) AS Examiner,
CONCAT(u2.FirstName, ' ', u2.LastName) AS Supervisor
FROM betyg_essays e
INNER JOIN betyg_users u1 ON e.Examiner = u1.UID
INNER JOIN betyg_users u2 ON e.Supervisor = u2.UID

我没有尝试代码,您可能需要进行一些调整。

更新:修复了id

INNER JOIN betyg_users u2 ON e.Supervisor = u2.UID

更新2:

我已将 + 更改为 CONCAT 函数。显然,MySQL 处理字符串的方式与 SQL Server 不同。

于 2012-06-10T11:13:48.953 回答
2

在这里,您需要加入 user_table 两次,第一次获取考官用户名,第二次获取主管用户名

SELECT (g1.FirstName + ' ' + g1.LastName) AS Examiner,' , (g2.FirstName + ' ' + g2.LastName) AS Supervisor
FROM grading_table grade
INNER JOIN user_table g1 ON grade.examiner = g1.uid
INNER JOIN user_table g2 ON grade.supervisor = g2.uid
于 2012-06-10T11:15:49.267 回答
2

最终解决方案:

$query = "SELECT 
            CONCAT (u1.Firstname, ' ', u1.Lastname) AS Examiner, 
            CONCAT (u2.Firstname, ' ', u2.Lastname) AS Supervisor
        FROM betyg_essays grade
            INNER JOIN betyg_users u1 ON grade.Examiner = u1.UID
            INNER JOIN betyg_users u2 ON grade.Supervisor = u2.UID";
于 2012-06-10T11:48:00.340 回答
1

试试这个

SELECT CONCAT(betyg_users.`Firstname`," ",betyg_users.`Lastname`) AS `examiner_name`,
CONCAT(betyg_users.`Firstname`," ",betyg_users.`Lastname`) AS `superviser_name`
FROM `betyg_essays`
INNER JOIN `betyg_users` ON betyg_essays.`Examiner` = betyg_users.`UID` 
INNER JOIN `betyg_users` ON betyg_essays.`Supervisor` = betyg_users.`UID`;
于 2012-06-10T11:15:41.000 回答