如何在mysql

时间:2015-09-09 06:00:56

标签: php mysql database

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

3 个答案:

答案 0 :(得分:0)

试试这种方式

select product_id , group_concat (`Size` separator ',') as `Size` from tablename group by product_id 

答案 1 :(得分:0)

$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'];
}


}

试试这个。

答案 2 :(得分:0)

您只需使用group_concatgroup by子句作为

Select product_id, group_concat(size) as size
from product_table group by product_id
order by id asc