选择两列中的distinct,但逐一列

时间:2014-11-04 22:53:11

标签: php html mysql

我需要从数据库结果中选择一个菜单,例如:

http://it.tinypic.com/r/2f092pv/8

记录sql示例:

字段 -------产品----宏观类别---微类别 - 价格----

价值 ------- Nvidia ------硬件-----图形卡---- 399 $ -----

我试过了:

$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>

结果:每个微类别结果的1 <li>个宏类别

请帮帮我......

1 个答案:

答案 0 :(得分:0)

一般来说,HTML中存在一些奇怪之处:<​​/ p>

// 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>