简体   繁体   中英

mysql multiple INNER JOIN to add two table columns to another table

three tables

status : 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)

submit : ID,student -->

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

New table to build from query:

 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 )

I'd like to make arrays of all students assigned to each member in status table
eg.
array1 --> member_Joseph: student 1, student 2, student 3
array2 --> member_John: student 2, student 4, student 5
array3 -->member_Jacob: student 3, student 5, student 1

Would that be:

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

The error is: "Not unique table/alias"

(fyi: I'm using Drupal 6 syntax)

I think it should be the table info should be joined twice so you can get the member_name of each memberID. You also need to supply an alias for the table so they can be uniquely identified, eg

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

To further gain more knowledge about joins, kindly visit the link below:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM