MySQL独特而左,右

时间:2011-04-29 17:05:56

标签: mysql sql distinct

我有一个带有日期列的表格,如下所示:

2011-04-28, 2011-04-29 ...
day           count  name surname
2011-04-28    8       titi tutu
2011-04-28     12     tutu toto
2011-04-27     2      tutu toto
2011-03-12     10     tutu toto

我可以获得截然不同的一天,但不仅仅是月份和年份。

select distinct(day) from Table where day between "2011-03-01" and "2011-04-28";

我只想要明确的月份和年份。

你能帮助我吗?

由于

2 个答案:

答案 0 :(得分:2)

select DISTINCT EXTRACT(YEAR_MONTH FROM `day`) as yearmonth
from Table
where day between '2011-03-01' and '2011-04-28';

答案 1 :(得分:1)

DISTINCT可能只适用于mysql中的整行。因此,您需要extract从日期开始所需的内容。

select distinct(EXTRACT YEAR_MONTH FROM `day`) from Table 
      where day between "2011-03-01" and "2011-04-28";