kacper
kacper

Reputation: 370

Get variables from query with JOIN

I'm sure that my problem is very stupid, but I don't know how to get data from query after JOIN, when I have columns with this same name in several tables used in query, ex.:

$query="SELECT * FROM `clients` JOIN `devices` ON `clients`.`id`=`devices`.`client_id`";
$devices = $Database->select($query);       
for($i=0; $i<sizeof($devices); $i++)
    {
        // I need to get 'name' from `devices` AND `clients` tables...
    }

Many thanks in advance.

Upvotes: 0

Views: 103

Answers (2)

arni
arni

Reputation: 78

First of all, it doesn't look like you need to select the whole table to begin with. So:

SELECT devices.name, clients.name
FROM clients JOIN devices ON clients.id=devices.client_id

Now you just need to access the data, one row at a time from $devices.

Upvotes: 0

Victor D. Castillo
Victor D. Castillo

Reputation: 111

You can use alias for table name and for columns

Example

SELECT c.name as clientName, d.name as deviceName 
FROM clients c JOIN devices d ON clients.id=devices.client_id

Then in your loop you can get "clientName" and "deviceName"

Upvotes: 2

Related Questions