[英]Select distinct in two column but group by one
I need to select from the database results for make a menu as in example: 我需要从数据库结果中选择一个菜单,例如:
http://it.tinypic.com/r/2f092pv/8 http://it.tinypic.com/r/2f092pv/8
Record sql example: 记录sql示例:
Field -------Product ----Macrocategories---Microcategories--Price---- 领域 -------产品----宏类---微类-价格----
Value -------Nvidia------ Hardware ----- Graphic Cards ---- 399$ ----- 价值 ------- NVIDIA ------硬件-----图形卡---- 399 $ -----
I tried this: 我尝试了这个:
$select_MENU = "select distinct macrocategories, microcategories from catalog";
$result_MENU = mysql_query($select_MENU);
if ($result_MENU)
while($dati = mysql_fetch_assoc($result_MENU))
{
echo "<li><a>$dati[macrocategories]</a>
<ul>
<li><a href='index.php?microcategories=$dati[microcategories]'>$dati[microcategories]</a></li>
</ul>";
}
?>
</ul>
The result: 1 <li>
Macrocategories for each result of microcategories 结果:每个微类别结果1个
<li>
宏类别
Help me please... 请帮帮我...
There are a couple oddities in your HTML in general: 通常,HTML中有一些奇怪的地方:
// You will want to switch to PDO or mysqli prepared statements for security and deprecated reasons
// Distinct will only select unique instances in your table
$select_MENU = "select distinct macrocategories, microcategories from catalog";
$result_MENU = mysql_query($select_MENU); ?>
<ul>
<?php
if($result_MENU) {
while($dati = mysql_fetch_assoc($result_MENU)) { ?>
<li><?php echo $dati['macrocategories']; ?></li>
<li><a href="index.php?microcategories=<?php echo $dati['microcategories']; ?>"><?php echo $dati['microcategories']; ?></a></li><?php
}
} ?>
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.