tags:

views:

43

answers:

3

Hi all, I am trying to make menu with multiple submenus.I need to make query that takes the result of preceding query and leaves only distinct results of some field.If i make new query each time the server bugs because the query searches through veri big database.

So in short something like that :select distinct(field) form(already made query) Is there any way in mysql or php tthat this can be done?

A: 

in php you can loop on the first query result an build an array containing distinct fields:

$result = mysql_query($sql);// result of 1st query

$arr = array();//an array that will contain distinct field values

while ($row = mysql_fetch_assoc($result)) {// for each row
    if (!in_array($row["field"], $arr))    // check if it's not in the array
        $arr[] = $row["field"]             // add it
}
najmeddine
A: 

Sorry but is there any other method of doing this because the server makes again big effort while searching through the results?

Stan
+1  A: 

Can you not just use a subquery? SELECT DISTINCT field FROM (SELECT * FROM menus WHERE ...) More information on subqueries in MySQL. A subquery will let you do the outer select, against the results of the inner select.

Mark Story