Sherif Sherif - 3 months ago 6
SQL Question

SQL Join fetching same name row

I've got 2 tables, a join, a

SELECT *
. Both tables contain the field
id
, but I need to explicitly access one in this way:

$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 ;
}


I tried

echo $row->d.id;


That didn't work..I know I could technically change my SELECT to call for the id's and use aliases but, there are a lot of fields I am fetching, hence the
*
. Is there another way?

Answer

You'll have to use aliases, the query stays short if you duplicate the data:

"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"

And then:

$row->did
$row->uid