I'm not even sure what to search for so forgive me if this is already covered somewhere.
I have a SELECT statement that pulls data from several tables. As expected if I run this query I get a record for each record in the second table, like this.
|ID |Name |Assets
------------------------------------
|1 |Bob |Car
|1 |Bob |Bicycle
|1 |Bob |House
|2 |Jane |Car
|2 |Jane |House
|3 |Peter |Boat
|3 |Peter |Car
|3 |Peter |Motorcycle
What I want is something like this:
|ID |Name |Assets
------------------------------------
|1 |Bob |Car,Bicycle,House
|2 |Jane |Car,House
|3 |Peter |Boat,Car,Motorcycle
I have been able to achieve this using PHP by simply hitting the database a second time for each record I get from the first table, but this has made my page slow as it's hitting the database some 40-60 times per page refresh.
Query 1:
SELECT ID,Name FROM People
Then Query 2 within a while loop:
<?php
while($owner_row=odbc_fetch_array($query1) {
$assets = odbc_exec($conn,"SELECT Asset FROM Assets WHERE Owner='$owner_row[Name]'");
$asset_array = odbc_fetch_array($assets);
$asset_string = implode(",",$asset_array);
}
I can then combine the Asset results from each owner into a string and display them as I like.
Is there a way to achieve this in a single query so that I only hit the database once and somehow combine those assets into a single value within the query itself?