Optimise two mysql queries to one query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dr zoidberg

    Optimise two mysql queries to one query

    Hello,

    $a = mysql_query("Se lect a,b FROM t WHERE category=1")
    while($a) {
    $x = $a[a];
    //some echo
    //another query

    $b = mysql_query("Se lect * 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
  • Robert Peake

    #2
    Re: Optimise two mysql queries to one query

    This just looks like:

    "select a,b,subcategory FROM t WHERE category=1 and subcategory=a"

    To me, then a loop on each row that checks if you've seen that category or
    subcategory before - if so, keep listing, if not, display the
    category/subcategory heading and indent all future output.

    Cheers,
    Robert

    --
    Robert Peake | Peake Professional Consulting
    Robert@PeakePro .com | http://www.peakepro.com/

    On 3/16/04 2:12 PM, in article sTK5c.6319$%k.4 0112@ns45.bih.n et.ba, "dr
    zoidberg" <user@domain.in vald> wrote:
    [color=blue]
    > Select a,b FROM t WHERE category=1[/color]


    Comment

    • Richard Allsebrook

      #3
      Re: Optimise two mysql queries to one query

      Here's how I handle the displaying of my category list on my blog:



      It uses recursion to build the category tree. You might find something
      there you can use.



      dr zoidberg wrote:[color=blue]
      > Hello,
      >
      > $a = mysql_query("Se lect a,b FROM t WHERE category=1")
      > while($a) {
      > $x = $a[a];
      > //some echo
      > //another query
      >
      > $b = mysql_query("Se lect * 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[/color]

      Comment

      • Perttu Pulkkinen

        #4
        Re: Optimise two mysql queries to one query

        "dr zoidberg" <user@domain.in vald> wrote:
        [color=blue]
        > Point is that I have menus, parents with childes (childes are not
        > parents) and I want to display them using only one query.[/color]

        You can also put them first in array and then order them using same kinds of
        technics than with database.


        Comment

        Working...