how to compare date using sql???

Discussion in 'PHP' started by rrn, May 10, 2009.

  1. #1
    Hi all ,

    in my database there are rows of date in the form dd-mm-yyyy
    wat i want to do is?
    i want to leave a space after different years..ie
    if the table is
    22-09-2004
    06-06-2004
    25-12-2005
    03-02-2005
    17-02-2008

    it should be like this

    22-09-2004
    06-06-2004

    25-12-2005
    03-02-2005

    17-02-2008

    how will i compare the years using php code??
     
    rrn, May 10, 2009 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    $res = mysql_query("SELECT EXTRACT(YEAR FROM jdate) AS year1 ,jdate FROM tablename") or die("Sql error: " . mysql_error());
    while($row = mysql_fetch_array($res)) {
    $y1=$row["year1"];
    if ($y2==$y1)
    echo $row["year1"] . "<br/>";
    else
    echo $row["year1"] . "<br/><br/><br/>";
    $y2=$row["year1"];
    }


    }
     
    kmap, May 10, 2009 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Hi,

    You have to GROUP BY YEAR(your_datetime_filed).
    Regards
     
    koko5, May 11, 2009 IP