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?
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
}
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.