简体   繁体   中英

how to mysqli inner join with 3 tables

In the end I need results to contain all data from all 3 tables. How to join the 3 tables so all is present in the end. Here is what I have so far:

if ($results = $db->query("SELECT * FROM HWData
                        LEFT JOIN ClassData
                        ON HWData.class = ClassData.class
                        LEFT JOIN Judges
                        ON Judges.groupnum = ClassData.groupnum
                        ORDER BY HWData.entrynum",
                        MYSQLI_USE_RESULT)) {
$result_set = mysqli_fetch_all($results, MYSQLI_ASSOC);
echo json_encode($result_set);

The results are not as expected. It's odd that console.log[i]['groupnum']; shows correct groupnum but $("#output").append("<p>"+data[i]['groupnum']); shows only 1's

3 tables as follows:

ClassData

groupnum    abbr
0   DQB
0   DQC
1   SUM
1   PP
1   FOS
1   VANF
1   VANS
2   AFCF
2   AFCS
2   ICCS
2   CCS
3   LSUF
3   LTMS
4   HL
4   MC

Judges

judge   group
Coleen  4
Daniel  3
Aaron   2
Jeff    1

HWData

entrynum    fname   class
1   Esteban FOS
2   Dalia   PP
3   Sheri
4   Sheri   HL
5   Sheri   MC
10  Danita  ICCS
11  Chris   AFCS

Ok, so i figured out you can use multiple joins.

if ($results = $db->query("SELECT * FROM HWData
                           LEFT JOIN ClassData
                           ON HWData.Abbr = ClassData.Abbr
                           LEFT JOIN Judges
                           ON Judges.groupnum = ClassData.groupnum",
                            MYSQLI_USE_RESULT)) {
$result_set = mysqli_fetch_all($results, MYSQLI_ASSOC);
echo json_encode($result_set);
$results->close();
}

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