简体   繁体   English

如何从 mySQL 中获取所有年份的列表

[英]how to get a list of all years from the mySQL

+------------+---------------+------------+
| date       | other_columns | date2      |
+------------+---------------+------------+
| 2019-05-23 | #             | 2018-04-12 |
+------------+---------------+------------+
| 2013-04-08 | #             | null       |
+------------+---------------+------------+
| 2007-11-11 | #             | 2019-09-13 |
+------------+---------------+------------+ 

there is such a table in mySQL, with several thousand rows. mySQL 中有这样一个表,有几千行。 how can I get out of there only years without requesting the entire base.我怎么能在不要求整个基地的情况下离开那里几年。 I just can't figure out how to do this.我只是不知道如何做到这一点。

[2019,
2018,
2013,
2007]

I would like to extract them something like this, I will be glad to any advice我想提取它们这样的东西,我很乐意提供任何建议

You can use year() and union all :您可以使用year()union all

select distinct year(date)
from (select date from t union all
      select date2 from t
     ) t;

If you want this as a single concatenated string, I would recommend:如果您希望将其作为单个串联字符串,我建议:

select group_concat(yyyy order by yyyy) as years
from (select year(date) as yyyy from t
      union -- intentionally remove duplicates
      select year(date2) from t
     ) t;

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

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