Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

mysqli - How to return mysql data using table aliases with left join and duplicate columns

I am trying to do a simple left join, while using table aliases, to return all of the values from both tables in this format:

[Alias1] => Array
    (
        [id] => 1
        [city] => bay area
        [state] => CA
    )

[Alias2] => Array
    (
        [id] => 1
        [city] => san francisco
        [baseball_team] => giants

     )

Here is my very simple mysqli_query:

$result = mysqli_query($sql, SELECT  Alias1.id, Alias1.city, Alias1.state, Alias2.id, Alias2.city, Alias2.baseball_team FROM database.table1 AS Alias1 LEFT JOIN database.table2 AS Alias2 ON Alias1.id = Alias2.id)

To get the results... I use:

while($row = mysqli_fetch_array($result))
{
     $data[] = $row;
}
print_r($data);die();

Problem: The data prints out as such:

 [0] => Array
        (
            [0] => 1
            [id] => 1
            [1] => bay area
            [city] => bay area
            [2] => CA
            [state] => CA
            [3] => 1
            [4] => san francisco
            [5] => giants
            [baseball_team] => giants
        )

Other Info: I originally tried to get the data using mysql_fetch_assoc($result)... But the duplicate columns don't return at all.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of:

$data[] = $row;

Use this to form the data array that you desire:

$data['Alias1'] = array(
    "id"=>$row['id'],
    "city"=>$row['1_city'],
    "state"=>$row['state'],
);
$data['Alias2'] = array(
    "id"=>$row['id'],
    "city"=>$row['2_city'],
    "baseball_team"=>$row['baseball_team'],
);

OR, if you expect more than one row you need to use something like:

$data[] = array(
    "Alias1" => array(
        "id"=>$row['id'],
        "city"=>$row['1_city'],
        "state"=>$row['state'],
    ),
    "Alias2" => array(
        "id"=>$row['id'],
        "city"=>$row['2_city'],
        "baseball_team"=>$row['baseball_team'],
    ),
);

Note: If your city values are different between your two tables then you should use an alias so that you can pull the other one differently. If they are the same, I suggest you not have them listed twice in the database as this is duplicating data and leaves your database denormalized.

Edit: Your query should be something like

SELECT  Alias1.id, Alias1.city AS 1_city, Alias1.state, Alias2.id, Alias2.city AS 2_city, Alias2.baseball_team FROM database.table1 AS Alias1 LEFT JOIN database.table2 AS Alias2 ON Alias1.id = Alias2.id

See edited answers above for these aliases.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...