PHP MySQL内连接具有相同的列名

时间:2014-06-30 13:38:59

标签: php mysql inner-join

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

表人

id

表格工作

id

在上面的代码中,我正在两个表之间进行内部连接。两个表都有一个名为id的列,是否有办法区分while loop中的两个?

我已经尝试了上述内容但$row['j.id']不起作用,如果我$row['id'],则它会将idjob_id写入相同的值。

1 个答案:

答案 0 :(得分:10)

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