mySQL中有一个这样的表,其中包含数千行。我如何才能在不要求整个基地的情况下仅离开那里几年。我只是不知道该怎么做。 我想像这样提取它们,我将很乐意提供任何建议 答案 0 :(得分:1) 您可以使用 如果要将其作为单个串联字符串,则建议:+------------+---------------+------------+
| date | other_columns | date2 |
+------------+---------------+------------+
| 2019-05-23 | # | 2018-04-12 |
+------------+---------------+------------+
| 2013-04-08 | # | null |
+------------+---------------+------------+
| 2007-11-11 | # | 2019-09-13 |
+------------+---------------+------------+
[2019,
2018,
2013,
2007]
1 个答案:
year()
和union all
:select distinct year(date)
from (select date from t union all
select date2 from t
) t;
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;