0

我不是 SQL 专家,但我正在使用以下 SELECT:

SELECT 
        certificate.id, 
        certificate.date_created,
        certificate.date_approved,
        certificate.notes,
        certificate.id_user_student,
        certificate.id_user_exm,
        certificate.id_user_eval,

        user.name AS student_name,
        user.name AS exm_name,
        user.name AS eval_name

        FROM certificate
        LEFT JOIN 
            user ON certificate.id_user_student = user.id AND
                    certificate.id_user_exm = user.id AND
                    certificate.id_user_eval = user.id

我的想法是能够name从表中取出列,user并将JOIN其放入certificate表中以放置所有用户 id 字段。

在表certificate中,列id_user_student, id_user_exm,id_user_eval包含表中可用的用户 ID user,我想在结果集中使用他们的名称。

我当前的查询仅适用于第一个结果。谁能告诉我如何解决这个问题?

谢谢!

4

1 回答 1

3

您将需要三个连接操作,一个用于基于 id 的名称的每个“查找”。您需要对用户表的三个引用,每个外键引用一个,例如:

SELECT c.id
     , c.date_created
     , c.date_approved
     , c.notes
     , c.id_user_student
     , c.id_user_exm
     , c.id_user_eval
     , s.name AS student_name
     , e.name AS exm_name
     , v.name AS eval_name
  FROM certificate c
  LEFT
  JOIN user s ON s.id = c.id_user_student
  LEFT
  JOIN user e ON e.id = c.id_user_exm
  LEFT
  JOIN user v ON v.id = c.id_user_eval
于 2013-07-12T19:49:01.110 回答