![](/img/trans.png)
[英]MySQL get sum of columns from two tables having multiple records of same product
[英]How to display same records all together having same product id in mysql
id product_id size
1 185 3
2 195 4
3 185 4
4 198 1
如果有多個記錄,我想將具有相同產品 ID 的大小記錄一起顯示。 如果它只是一個產品 id 顯示尺寸。
我試過這個:
$qq = mysql_query("select product_id,GROUP_CONCAT(size SEPRATOR ',')AS
size from sizes group by product_id");
while ($row = mysql_fetch_array($qq)) {
$ad = $row['size'];
}
我收到此錯誤:
error:mysql_fetch_array() expects parameter 1 to be resource,
boolean given in /home/clebster/public_html/fun.php
試試這個方法
select product_id , group_concat (`Size` separator ',') as `Size` from tablename group by product_id
$sql="SELECT size FROM tbl WHERE id='$product_id'";
$res=mysql_query($sql);
$num_rows=myqli_num_rows($res);
if($num_rows>0)
{
for($i=0; $i<$num_rows;$i++)
{
$data=mysqli_fetch_array($res);
echo $data['size'];
}
}
嘗試這個。
您可以簡單地將group_concat
與group by
子句一起使用
Select product_id, group_concat(size) as size
from product_table group by product_id
order by id asc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.