Syntax for min and max

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

    Syntax for min and max

    I wish to assign a variable name to the MIN and MAX values of a field
    in a table. The type is int(7).

    I am extracting the field with many others with the following code
    from a php call.

    $ml_collect='SE LECT * FROM ml_lopp LEFT JOIN scfmforening ON
    (scfmforening.s cfmnum=ml_lopp. scfmnum) LEFT JOIN ml_newtidplats ON
    (ml_newtidplats .loppnum=ml_lop p.loppnum) ORDER BY date1,
    ml_lopp.loppnum ';

    $ml_upg=mysql_q uery($ml_collec t);

    mysql_close();

    The field is contained within the table ml_lopp and it is called
    loppnum

    I have been trying combinations of this to extract the min and max of
    the field from the $ml_upg variable which includes all of the data to
    be processed.

    $lownum=mysql_r esult($ml_upg,M IN('loppnum'));
    $hghnum=mysql_r esult($ml_upg,M AX('loppnum')); ;

    Which does not work.

    I am very greafull of any help in this matter.

    Garry Jones
    Sweden

  • lorento

    #2
    Re: Syntax for min and max

    $ml_collect='SE LECT MIN(ml_lopp.lop pnum) as minNum, MAX
    (ml_lopp.loppnu m) as maxNum FROM ml_lopp LEFT JOIN scfmforening ON
    (scfmforening.s cfmnum=ml_lopp. scfmnum) LEFT JOIN ml_newtidplats ON
    (ml_newtidplats .loppnum=ml_lop p.loppnum) ORDER BY date1,
    ml_lopp.loppnum ';

    $ml_upg=mysql_q uery($ml_collec t);

    while ($row = mysql_fetch_obj ect ($ml_upg))
    {
    $min = $row->minNum;
    $max = $row->maxNum;
    }
    ---



    On Feb 7, 7:59 am, "GarryJones " <mor...@algonet .sewrote:
    I wish to assign a variable name to the MIN and MAX values of a field
    in a table. The type is int(7).
    >
    I am extracting the field with many others with the following code
    from a php call.
    >
    $ml_collect='SE LECT * FROM ml_lopp LEFT JOIN scfmforening ON
    (scfmforening.s cfmnum=ml_lopp. scfmnum) LEFT JOIN ml_newtidplats ON
    (ml_newtidplats .loppnum=ml_lop p.loppnum) ORDER BY date1,
    ml_lopp.loppnum ';
    >
    $ml_upg=mysql_q uery($ml_collec t);
    >
    mysql_close();
    >
    The field is contained within the table ml_lopp and it is called
    loppnum
    >
    I have been trying combinations of this to extract the min and max of
    the field from the $ml_upg variable which includes all of the data to
    be processed.
    >
    $lownum=mysql_r esult($ml_upg,M IN('loppnum'));
    $hghnum=mysql_r esult($ml_upg,M AX('loppnum')); ;
    >
    Which does not work.
    >
    I am very greafull of any help in this matter.
    >
    Garry Jones
    Sweden

    Comment

    • Steve

      #3
      Re: Syntax for min and max

      On Tue, 06 Feb 2007 21:49:12 -0800, lorento wrote:
      $ml_collect='SE LECT MIN(ml_lopp.lop pnum) as minNum, MAX
      (ml_lopp.loppnu m) as maxNum FROM ml_lopp LEFT JOIN scfmforening ON
      (scfmforening.s cfmnum=ml_lopp. scfmnum) LEFT JOIN ml_newtidplats ON
      (ml_newtidplats .loppnum=ml_lop p.loppnum) ORDER BY date1,
      ml_lopp.loppnum ';
      >
      $ml_upg=mysql_q uery($ml_collec t);
      >
      while ($row = mysql_fetch_obj ect ($ml_upg))
      {
      $min = $row->minNum;
      $max = $row->maxNum;
      }
      >
      I'd suggest something along the lines of...

      $row = mysql_fetch_obj ect ($ml_upg);
      $min = $row->minNum;
      $max = $row->maxNum;

      while ($row = mysql_fetch_obj ect ($ml_upg))
      {
      $min = ($row->minNum < $min) ? $row->minNum : $min;
      $max = ($row->maxNum $max) ? $row->maxNum : $max;
      }

      Would work better???

      Steve

      Comment

      • Toby A Inkster

        #4
        Re: Syntax for min and max

        Steve wrote:
        Would work better???
        Would work slower.

        Assuming that "ml_lopp.loppnu m" is indexed, then the database engine ought
        to be able to pick out the minimum and maximum values a lot faster than
        PHP will be able to. Even without indexes, chances are the database engine
        will beat PHP.

        --
        Toby A Inkster BSc (Hons) ARCS
        Contact Me ~ http://tobyinkster.co.uk/contact
        Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

        * = I'm getting there!

        Comment

        Working...