MySQL : one query for join multiple tables and store result in 3arrays - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Tuesday, March 15, 2022

MySQL : one query for join multiple tables and store result in 3arrays

I'm trying to write one query for joining 3 tables without link, I was trying with UNION SQL command like this:

SELECT *
FROM 
    (SELECT 
         a.id AS field_aa, a.column2 AS field_ab, a.column3 AS field_ac
     FROM tableA a
     UNION
     SELECT 
         b.id AS field_ba, b.column2 AS field_bb, b.column3 AS field_bc
     FROM tableB b
     UNION
     SELECT 
         c.id AS field_ca, c.column2 AS field_cb, c.column3 AS field_cc
     FROM tableC c) abc
WHERE 1;

And after i want to fill 3 arrays for each table

while( ($arr = $_opDB->fetch_assoc($result)) != FALSE ) {
        $array_one = array(
            'field_id' => $arr['field_aa'],
            'field_one' => $arr['field_ab'],
            'field_two' => $arr['field_ac'],
        ) ;
        $array_two = array(
            'field_id' => $arr['field_ba'],
            'field_one' => $arr['field_bb'],
            'field_two' => $arr['field_bc'],
        ) ;
        $row_three = array(
            ...
        ) ;
        $global_array['firstSelect'][] = $array_one ;
        $global_array['secondSelect'][] = $array_two ;
        $global_array['thirdSelect'][] = $array_three ;
    }

All my entries are added in $global_array['firstSelect'], others are empty



source https://stackoverflow.com/questions/71469693/mysql-one-query-for-join-multiple-tables-and-store-result-in-3arrays

No comments:

Post a Comment