Re: Optimise two mysql queries to one query

From: Date: Tue, 16 Mar 2004 22:22:35 +0000
Subject: Re: Optimise two mysql queries to one query
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-180613@lists.php.net to get a copy of this message
Dr. Zoidberg wrote:
Hello, $a = mysql_query("Select a,b FROM t WHERE category=1") while($a) { $x = $a[a]; //some echo //another query $b = mysql_query("Select * FROM t WHERE subcategory=$x") while ($b) { //some echo } } Point is that I have menus, parents with childes (childes are not parents) and I want to display them using only one query. - Parent 1 -- Child 1 -- Child 2 - Patent 2 -- Child 3 Child have subid that is parent id. TNX
SELECT p.a AS pa, p.b AS pb, c.a AS ca, c.b AS cb FROM t AS p LEFT JOIN t AS c ON c.subcategory = p.a WHERE p.category=1 ORDER BY p.category This should give you what you want, but the two queries might be better speed wise.... -- paperCrane <Justin Patrin>

« previous php.general (#180613) next »