I am having the following script that takes the results from 3 tables based on a field called “order_id” in all tables.
$sql = "SELECT
o.order_id,
order_product.model,
o.name,
o.value,
GROUP_CONCAT(o.order_id, order_product.model, o.name, o.value SEPARATOR ',') AS Options
FROM `order_option` AS o
LEFT JOIN `order` AS oo on o.order_id = oo.order_id
LEFT JOIN order_product ON o.order_id = order_product.order_id
where oo.order_status_id = 2
group by oo.order_id";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
$options=$row['Options'];
print "|$options|";
PRINT "<br>-------------------<br>";
}
The problem is that in table “order_option” there are 2 or more rows with options for each order_id.
The results printed by the script is:
ID, Model, option1:Value, ID, Model, option2:Value
And what I need is:
ID, MODEL, OPTION1:Value, OPTION2:Value
So, no duplication of ID and MODEL for every row.
Any suggestions how can I achieve that?
The problem is with how you’re implementing
GROUP_CONCAT. You have 2 rows in yourOrder_Optiontable that have different options with the same option id. Something like:Using GROUP_CONCAT(OrderId, Name, Value) will concatenate all those fields like:
Instead, you want to retrieve your OrderId on it’s own:
This will give you:
And then the rest is just in your presentation logic.
Good luck.