简体   繁体   中英

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

Record sql example:

Field -------Product ----Macrocategories---Microcategories--Price----

Value -------Nvidia------ Hardware ----- Graphic Cards ---- 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

Help me please...

There are a couple oddities in your HTML in general:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM