MySQL和PHP:如何从下面的例子中安排数据

时间:2014-03-12 09:48:40

标签: php mysql

MySQL代码如下,

表中有MySQL数据库

enter image description here

实际上,

唯一商品组ID 3 (分别为101,102和103) 总物料组ID有23个

101 = 10

102 = 5

103 = 8

总计= 23

每个组ID(10或5或8)总数除以唯一组(3)。

round(10/3)= 3,round(5/3)= 2 and round(8/3)= 3

在输出中首先101依次顺序3次,然后依次顺序2次102次,顺序依次顺次3次3次,再次保持上述比率仍然打印所有数据。

输出将如下图所示

enter image description here

请使用MySQL或PHP代码提供答案,

$result = mysqli_query($con,"SELECT * FROM `item` order by(item_group_id)");

echo "<table><tr><th>ITEM GROUP ID</th><th>ITEM NAME</th></tr>";
while($row = mysqli_fetch_array($result))
{
 ehco "<tr><td>";
 echo $row['item_group_id'] 
 echo "</td><td>";
 echo $row['item_name'];

 echo "</td></tr>";
}
echo "</table>";

SQLFiddle

1 个答案:

答案 0 :(得分:-3)

$result = mysqli_query($con,"SELECT DISTINCT item_group_id,item_name  FROM `item` order by(item_group_id)");

echo "<table><tr><th>ITEM GROUP ID</th><th>ITEM NAME</th></tr>";
while($row = mysqli_fetch_array($result))
{
 ehco "<tr><td>";
 echo $row['item_group_id'] 
 echo "</td><td>";
 echo $row['item_name'];

 echo "</td></tr>";
}
echo "</table>";