简体   繁体   中英

PHP MySQL inner join with same column name

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

$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']
    );
}

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