SQL Join获取同名行

时间:2012-01-19 19:15:37

标签: php sql join

我有2个桌子,一个联接,一个SELECT *。两个表都包含字段id,但我需要以这种方式显式访问:

$query = "SELECT * FROM  #__docman as d JOIN #__users u ON d.dmmantainedby = u.id WHERE d.catid = 5 ORDER BY d.id ASC";
$db->setQuery($query);
$rows = $db->loadObjectList();

foreach($rows as $row) {
    echo $row->id ; 
}

我试过

echo $row->d.id;

那不起作用..我知道我可以在技术上改变我的SELECT以调用id并使用别名但是,我有很多字段,因此*。还有另一种方式吗?

2 个答案:

答案 0 :(得分:2)

您必须使用别名,如果您复制数据,查询将保持简短:

"SELECT *, d.id as did, u.id as uid FROM  #__docman as d JOIN #__users u ON d.dmmantainedby = u.id WHERE d.catid = 5 ORDER BY d.id ASC"

然后:

$row->did
$row->uid

答案 1 :(得分:0)

SELECT *, d.id AS id_alias FROM ...?这将选择重复的列,但仍然是非常简短的查询。