Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL inner join with same column name [duplicate]

$q = $db->query("SELECT * 
        FROM people p
        INNER JOIN job j           
        ON p.job_id = j.id
        WHERE p.id = '$id'
        ORDER BY j.id ASC");
    $maps = array();
    while($row = mysqli_fetch_array($q)) {
        $product = array(
            'id' => $row['id'],
            'job_id' => $row['j.id']
        )
    }

table people

id

table job

id

In the above code I am doing an inner join between two tables. Both tables have an a column called id is there a way to differentiate between the two in my while loop?

I have tried the above but $row['j.id'] doesn't work and if I do $row['id'] then it writes both id and job_id with the same value.

like image 588
ngplayground Avatar asked Dec 05 '22 06:12

ngplayground


1 Answers

$q    = $db->query("SELECT *, j.id as jid, p.id as pid 
        FROM people p
        INNER JOIN job j           
        ON p.job_id = j.id
        WHERE p.id = '$id'
        ORDER BY j.id ASC");
$maps = array();
while ($row = mysqli_fetch_array($q)) {
    $product = array(
        'id'     => $row['pid'],
        'job_id' => $row['jid']
    );
}
like image 109
colburton Avatar answered Dec 15 '22 06:12

colburton