简体   繁体   中英

SQL to get DISTINCT date values from a table

I have a field in a table named startdate and what I would like to do is run as little queries as possible to obtain a list that would display as the following:

2012

January
March
October

2011

September
November

The only dates that I want to show per year are the dates where there is a record for.

Any help or pointers appreciated.

Thanks

This query can be used to find all distinct Year/Month combinations in a table for a given date (here start date).

SELECT     YEAR(startdate) AS DYear, MONTH(startdate) AS DMonth 
FROM         tablename
GROUP BY YEAR(startdate), MONTH(startdate) 

After you have your results back in whatever way you choose to get them you can do something like this:

$year = 0;
  while ($row) { //loop through your rows here using while or foreach
    if($year != $row['DYear']){
      echo '<h1>'.$row['DYear'].'</h1>';
      $year = $row['DYear'];
    }
    echo '<ul>';
    echo '<li>'.$row['DMonth'].'</li>';
    echo '</ul>';
  }

Using PDO , you could do something like:

$dbh = new PDO("mysql:dbname=$dbname", $username, $password);

$qry = $dbh->query('
  SELECT DISTINCT
    YEAR(startdate)      AS year,
    MONTHNAME(startdate) AS month
  FROM
    my_table
  ORDER BY
    startdate
');

if ($qry) {    
  $row = $qry->fetch();
  while ($row) {
    $current_year = $row['year'];
    echo '<h1>',htmlentities($current_year),'</h1><ul>';
    do {
      echo '<li>',htmlentities($row['month']),'</li>';
    } while ($row = $qry->fetch() and $row['year'] == $current_year);
    echo '</ul>';
  }
}
select distinct column1, column2, column3... from table where columnx <> "" order by year, month
SELECT DISTINCT YEAR(startdate), MONTHNAME(startdate)
FROM   mytable
ORDER BY YEAR(startdate) desc,  MONTH(startdate) asc;

should do the trick, however the output will be:

2012 January
2012 March
2012 October
2011 September
2011 November

you can use the code given by eggyal to convert this into a format that you are looking for. Note that you will need to order on MONTH and not MONTHNAME (unless you want alphabetical order)

As other posts have given your answer, I am going to provide information on why these answers work.

In an SQL select statement you can provide keywords. Specifically for MySQL you can provide ALL, DISTINCT, DISTINCTROW (and others unrelated to distinct rows).

The latter two options are actually the same option and yield the same results. The default select statement with no keyword uses ALL which returns all results. By passing in DISTINCT you eliminate any duplicate entries.

A duplicate entry is an entry where all the fields are the same, However, it should be noted that if you have an auto-incrementing primary key each row is distinct from the last as the pk is different. In order to select a distinct value with this kind of setup, you would need to specify a separate column name that is truly distinct.

For more reading on MySQL SELECT statements refer to the users guide's select section .

Hopefully I didn't provide information you have already gathered from the provided answers, but I have always found understanding "why" and "how" often allow me to understand when a particular solution will work, as well as when it won't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM