Sherif
Sherif

Reputation: 359

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?

Upvotes: 0

Views: 268

Answers (2)

Konrad Dzwinel
Konrad Dzwinel

Reputation: 37913

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

Upvotes: 2

Sergii Kudriavtsev
Sergii Kudriavtsev

Reputation: 10512

SELECT *, d.id AS id_alias FROM ... ? This will select duplicate columns but will still be pretty short query.

Upvotes: 0

Related Questions