简体   繁体   English

SQL从表中获取DISTINCT日期值

[英]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: 我在一个名为startdate的表中有一个字段,我想要做的是尽可能少地运行查询以获得一个显示如下的列表:

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: 使用PDO ,您可以执行以下操作:

$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. 您可以使用eggyal提供的代码将其转换为您正在寻找的格式。 Note that you will need to order on MONTH and not MONTHNAME (unless you want alphabetical order) 请注意,您需要在MONTH而不是MONTHNAME上订购(除非您想要按字母顺序排列)

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. 在SQL select语句中,您可以提供关键字。 Specifically for MySQL you can provide ALL, DISTINCT, DISTINCTROW (and others unrelated to distinct rows). 特别是对于MySQL,您可以提供ALL, DISTINCT, DISTINCTROW (以及与不同行无关的其他内容)。

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. 没有关键字的默认select语句使用ALL返回所有结果。 By passing in DISTINCT you eliminate any duplicate entries. 通过传入DISTINCT您可以消除任何重复的条目。

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. 重复条目是所有字段都相同的条目。但是,应该注意的是,如果您有一个自动递增的主键,则每行与最后一行不同,因为pk是不同的。 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 . 有关MySQL SELECT语句的更多阅读,请参阅用户指南的选择部分

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. 希望我没有提供您已经从提供的答案中收集的信息,但我总是发现理解“为什么”和“如何”经常让我理解特定解决方案何时起作用,以及什么时候不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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