Shermaine Chaingan Shermaine Chaingan - 2 months ago 10
PHP Question

Group by and show specific row value


I have this code to output the value in table i used group by to
group the same item code but serial does not show it only show
onevalue the 123321. The output should be 123321,4354


$invoicequery=mysqli_query($link,"Select SUM(INVOICE_ITEM_QTY_MX),INVOICE_ID_MX,INVOICE_ITEM_UPRICE_MX,INVOICE_ITEM_AMOUNT_MX,INVOICE_ITEM_SERIAL_MX,INVOICE_ITEM_DESC_MX,INVOICE_ITEM_CODE_MX from invoice WHERE BRANCH_CODE_MX='".$display_branchcode."'
and INVOICE_NO_MX='".$invoicecode."' GROUP BY INVOICE_ITEM_CODE_MX");
while($row=mysqli_fetch_array($invoicequery))
{
$invoiceid=$row["INVOICE_ID_MX"];
$itemcode=$row["INVOICE_ITEM_CODE_MX"];
$itemquantity=$row['SUM(INVOICE_ITEM_QTY_MX)'];
$unitprice=$row["INVOICE_ITEM_UPRICE_MX"];
$amount=$row["INVOICE_ITEM_AMOUNT_MX"];
$serialimei=$row["INVOICE_ITEM_SERIAL_MX"];
$itemdescription=$row["INVOICE_ITEM_DESC_MX"];

}


Ouput
Ouput
In Database
enter image description here

Answer

It sounds like you are looking for MYSQL's GROUP_CONCAT() function method. Simply wrap the column name you wish to have returned as a comma separated list with that function like you have used MAX().

Example:

SELECT student_name,
GROUP_CONCAT(test_score)
FROM student
GROUP BY student_name;

This will return, for example,

student_name | test_score Becky C. | 80, 77, 95

Note that this value will be truncated according to your db's group_concat_max_len setting.