mysql多个INNER JOIN将两个表列添加到另一个表中

时间:2013-02-20 06:06:12

标签: php mysql drupal select inner-join

三个表格

状态:ID,member_1,member_2 - >

(eg. 12, member_Joseph, member_John ; 
     22, member_Jacob, member_Jarrod ; 
     31, member_Jarrod, member_John 
     11, member_John, member_Jacob)

提交:身份证明,学生 - >

(eg. 12, Amy ; 
     22, Brian; 
     31 Susan ; 
     11 Kyle) 

info:member_name,contact - >

(eg. member_Joseph, joseph@name.name; 
     member_Jacob, jacob@name.name ; 
     member_Jarrod, jarrod@name.name ; 
     member_John, john@name.name )

要从查询构建的新表:

 member_Joseph, joseph@name.name, Amy; 
 member_Jacob, jacob@name.name, Kyle ; 
 member_Jarrod, jarrod@name.name, Brian, Susan ; 
 member_John, john@name.name, Amy, Susan, Kyle )

我想在状态表中为每个成员分配所有学生的数组 例如。
array1 - > member_Joseph:学生1,学生2,学生3       array2 - > member_John:学生2,学生4,学生5        array3 - > member_Jacob:学生3,学生5,学生1

那会是:

$query = db_query("
    SELECT info.member_name, submit.student  
    FROM {info} 
    INNER JOIN {status} 
    ON info.member_name = status.member_1
    INNER JOIN {status}
    ON info.member_name = status.member_2
    INNER JOIN {submit}
    ON submit.id = status.id
");
while($result = db_fetch_array($query)){
echo $result['student']; //iterate through members and print students for each
}

错误是:“不唯一的表/别名”

(fyi:我正在使用Drupal 6语法)

1 个答案:

答案 0 :(得分:0)

我认为应该将表info连接两次以便您可以获取每个memberID的member_name。您还需要为表提供别名,以便可以唯一标识它们,例如

SELECT  a.*, 
        b.member_name memberName1,
        c.member_name memberName2,
        // other columns you might want to show
FROM    status a        
        INNER JOIN info b
            ON a.member_1 = b.member_name
        INNER JOIN info c
            ON a.member_2 = c.member_name
        INNER JOIN submit d
            ON a.ID = d.ID

要进一步了解联接,请访问以下链接: