Php mysql分组优化
我有一张这样的桌子 表名:Php mysql分组优化,php,mysql,Php,Mysql,我有一张这样的桌子 表名:产品 +----+-------------+-----------+ | id | name | type | | 1 | apple | fruit | | 2 | banana | fruit | | 3 | tomato | vegetable | | 4 | egg plant | vegetable | | 5 | carrot | vegetable
产品
+----+-------------+-----------+
| id | name | type |
| 1 | apple | fruit |
| 2 | banana | fruit |
| 3 | tomato | vegetable |
| 4 | egg plant | vegetable |
| 5 | carrot | vegetable |
| 6 | Minced Steak| meat |
| 7 | Pork ribs | meat |
+----+-------------+-----------+
我希望输出html,如:
<ul>
<li>apple</li>
<li>banana</li>
</ul>
<ul>
<li>tomato</li>
<li>egg plant</li>
<li>carrot</li>
</ul>
<ul>
<li>Minced Steak</li>
<li>Pork ribs</li>
</ul>
- 苹果
- 香蕉
- 西红柿
- 茄子
- 胡萝卜
- 碎牛排
- 排骨
我不知道是否有一个简单的mysql查询。我的复杂mysql查询如下:
$result = mysql_query("SELECT * FROM products group by type ORDER BY type");
while ($data = mysql_fetch_array($result)){
echo '<ul>';
$querys = "select * FROM products WHERE type = ".$data['type']." ";
$results = mysql_query($querys);
while($row = mysql_fetch_array($results)){
echo '<li>'.$row['name'].'</li>';
}
echo '</ul>';
}
$result=mysql\u查询(“按类型从产品组中选择*);
而($data=mysql\u fetch\u数组($result)){
回声“”;
$querys=“从产品中选择*,其中type=“.$data['type']”;
$results=mysql\u查询($querys);
while($row=mysql\u fetch\u数组($results)){
回显“- ”.$row[“name]”。
;
}
回声“
”;
}
显然这是低效的,如何改进?您只需要第一次查询
$result = mysql_query("SELECT type, name FROM products ORDER BY type");
$grouped_data = array();
while ($data = mysql_fetch_array($result)){
$grouped_data[$data['type']][] = $data['name'];
}
现在,$grouped_data
给出了您想要的结果数组
对于输出,它也很简单:
foreach ($grouped_data as $data) {
echo '<ul><li>' . join('</li><li>', $data) . '</li></ul>';
}
foreach($data为$data){
回显“- ”。加入(“
- ”,$data)。“
”;
}
您只需要第一个查询
$result = mysql_query("SELECT type, name FROM products ORDER BY type");
$grouped_data = array();
while ($data = mysql_fetch_array($result)){
$grouped_data[$data['type']][] = $data['name'];
}
现在,$grouped_data
给出了您想要的结果数组
对于输出,它也很简单:
foreach ($grouped_data as $data) {
echo '<ul><li>' . join('</li><li>', $data) . '</li></ul>';
}
foreach($data为$data){
回显“- ”。加入(“
- ”,$data)。“
”;
}
如果要直接编写HTML,请使用组更改循环:
$oldgroup='__invalid__';
$needsendul=false;
$result = mysql_query("SELECT * FROM products ORDER BY type");
while ($data = mysql_fetch_array($result)) {
//Group change?
$newgroup=$data['type'];
if ($newgroup!=$oldgroup) {
if ($needsendul) echo '</ul>';
echo '<ul>';
$needsendul=true;
$oldgroup=$newgroup;
}
echo '<li>'.$row['name'].'</li>';
}
//Final closing tag
echo '</ul>';
$oldgroup=''无效'';
$needsendul=false;
$result=mysql_查询(“按类型从产品订单中选择*);
而($data=mysql\u fetch\u数组($result)){
//团队变革?
$newgroup=$data['type'];
如果($newgroup!=$oldgroup){
如果($needsendul)回显“”;
回声“”;
$needsendul=true;
$oldgroup=$newgroup;
}
回显“- ”.$row[“name]”。
;
}
//最后结束标记
回声“
”;
如果要直接编写HTML,请使用组更改循环:
$oldgroup='__invalid__';
$needsendul=false;
$result = mysql_query("SELECT * FROM products ORDER BY type");
while ($data = mysql_fetch_array($result)) {
//Group change?
$newgroup=$data['type'];
if ($newgroup!=$oldgroup) {
if ($needsendul) echo '</ul>';
echo '<ul>';
$needsendul=true;
$oldgroup=$newgroup;
}
echo '<li>'.$row['name'].'</li>';
}
//Final closing tag
echo '</ul>';
$oldgroup=''无效'';
$needsendul=false;
$result=mysql_查询(“按类型从产品订单中选择*);
而($data=mysql\u fetch\u数组($result)){
//团队变革?
$newgroup=$data['type'];
如果($newgroup!=$oldgroup){
如果($needsendul)回显“”;
回声“”;
$needsendul=true;
$oldgroup=$newgroup;
}
回显“- ”.$row[“name]”。
;
}
//最后结束标记
回声“
”;
使用分组方式和分组方式
$sql = "SELECT type, GROUP_CONCAT(name) AS names FROM product GROUP BY type";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
echo "<ul>";
$exploded = explode(',', $row['names']);
foreach($exploded as $name){
echo "<li>$name</li>";
}
echo "</ul>";
}
$sql=“按类型从产品组中选择类型、组(名称)作为名称”;
$result=mysql\u查询($sql);
while($row=mysql\u fetch\u assoc($result)){
回声“”;
$spolded=分解(“,”,$row['names']);
foreach($分解为$name){
echo“- $name
”;
}
回声“
”;
}
使用分组方式和分组方式
$sql = "SELECT type, GROUP_CONCAT(name) AS names FROM product GROUP BY type";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
echo "<ul>";
$exploded = explode(',', $row['names']);
foreach($exploded as $name){
echo "<li>$name</li>";
}
echo "</ul>";
}
$sql=“按类型从产品组中选择类型、组(名称)作为名称”;
$result=mysql\u查询($sql);
while($row=mysql\u fetch\u assoc($result)){
回声“”;
$spolded=分解(“,”,$row['names']);
foreach($分解为$name){
echo“- $name
”;
}
回声“
”;
}
首先获取数据并制作一个2D数组,如下所示:
$result = mysql_query("SELECT id,type, name FROM products ORDER BY type");
$data = array();
while ($row = mysql_fetch_array($result)){
$data[$row['type']][$row['id']] = $row['name'];
}
foreach ($data as $types=>$namesarray) {
echo '<ul>';
foreach($namesarray as $Key=>$Val){
echo '<li>'.$namesarray[$types][$Key]."</li>";
}
echo '</ul>';
}
要在
中打印,
请执行以下操作:
$result = mysql_query("SELECT id,type, name FROM products ORDER BY type");
$data = array();
while ($row = mysql_fetch_array($result)){
$data[$row['type']][$row['id']] = $row['name'];
}
foreach ($data as $types=>$namesarray) {
echo '<ul>';
foreach($namesarray as $Key=>$Val){
echo '<li>'.$namesarray[$types][$Key]."</li>";
}
echo '</ul>';
}
foreach($type形式的数据=>$namesarray){
回声“”;
foreach($namesarray作为$Key=>$Val){
回显“- ”。$namesarray[$types][$Key]。“
”;
}
回声“
”;
}
首先获取数据并制作一个2D数组,如下所示:
$result = mysql_query("SELECT id,type, name FROM products ORDER BY type");
$data = array();
while ($row = mysql_fetch_array($result)){
$data[$row['type']][$row['id']] = $row['name'];
}
foreach ($data as $types=>$namesarray) {
echo '<ul>';
foreach($namesarray as $Key=>$Val){
echo '<li>'.$namesarray[$types][$Key]."</li>";
}
echo '</ul>';
}
要在
中打印,
请执行以下操作:
$result = mysql_query("SELECT id,type, name FROM products ORDER BY type");
$data = array();
while ($row = mysql_fetch_array($result)){
$data[$row['type']][$row['id']] = $row['name'];
}
foreach ($data as $types=>$namesarray) {
echo '<ul>';
foreach($namesarray as $Key=>$Val){
echo '<li>'.$namesarray[$types][$Key]."</li>";
}
echo '</ul>';
}
foreach($type形式的数据=>$namesarray){
回声“”;
foreach($namesarray作为$Key=>$Val){
回显“- ”。$namesarray[$types][$Key]。“
”;
}
回声“
”;
}
在循环中运行查询是错误的。当您已经拥有所需的所有数据时,在循环中运行查询更糟糕。如果调整循环以在每次迭代中跟踪类型,并在类型更改时采取适当的操作,则无需重新获取循环中的数据
$type = '';
while ($data = mysql_fetch_assoc ($result))
{
if ($type != $data ['type'])
{
$type = $data ['type'];
// Take whatever action you need to take when the type changes here
}
// Do the usual output logic here
}
在循环中运行查询是不好的。当您已经拥有所需的所有数据时,在循环中运行查询更糟糕。如果调整循环以在每次迭代中跟踪类型,并在类型更改时采取适当的操作,则无需重新获取循环中的数据
$type = '';
while ($data = mysql_fetch_assoc ($result))
{
if ($type != $data ['type'])
{
$type = $data ['type'];
// Take whatever action you need to take when the type changes here
}
// Do the usual output logic here
}
@为什么
$grouped_data
是数组的数组。谢谢,我会尝试,但它还需要两个查询。。。还有更有效的方法吗?@fishman为什么还需要两个查询…我的代码只使用一个查询。您按类型分组,但一个类型包含多个产品。但是,因为您正在分组,所以对于每种类型,结果集只包含一行。无法返回包含数组的结果集。你能做的就是把它们分组,然后分解。看我的answer@AndyPieters-此查询只需删除分组依据
。解决方案很简单,使用explode+GROUP_CONCAT只需。。“傻!”安德烈说,“为什么$grouped_data
是数组的数组。谢谢,我会尝试,但它还需要两个查询。。。还有更有效的方法吗?@fishman为什么还需要两个查询…我的代码只使用一个查询。您按类型分组,但一个类型包含多个产品。但是,因为您正在分组,所以对于每种类型,结果集只包含一行。无法返回包含数组的结果集。你能做的就是把它们分组,然后分解。看我的answer@AndyPieters-此查询只需删除分组依据
。解决方案很简单,使用explode+GROUP_CONCAT只需。。愚蠢。-1这也将导致每种类型只有1种产品。您不能按类型分组并获取所有名称,您必须分组\u concat-1这也将导致每种类型仅1个产品。您不能按类型分组并获取所有名称,您必须分组\u concat-1这也将导致每种类型仅1个产品。你不能按类型分组并得到所有的名字,哟