0

Hey guys I want to get a month list which contain the months between 2 given month. For ex if i give month from January to June. Then I should be able to get all the months between them. I want to use those months in a repeater header. Can you tell me the query

select month(str_to_date('January','%M')); -- 1
select month(str_to_date('June','%M')); -- 6

Through the above code I will get the month numbers now i want to get the months between these 2.

4

4 に答える 4

0

月が同じ年の場合、次のようなクエリが必要だと思います。

SELECT
  DATE_FORMAT(STR_TO_DATE(months.m, '%m'), '%M')
FROM
  (SELECT 1 m UNION ALL SELECT 2 UNION ALL SELECT 3
   UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6
   UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
   UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12) months
WHERE
  months.m > month(str_to_date('January','%M'))
  AND months.m < month(str_to_date('June','%M'));

ここでフィドルを参照してください。ただし、状況によっては、コードで月の名前を取得する方が簡単な場合があります (たとえば、PHP や別のスクリプトを使用して...)。

于 2013-06-28T09:08:37.380 に答える
0

これを試して

   SELECT  month(str_to_date(`your_date_column`,'%M')) as my_month
   FROM `your_table`
   WHERE month(str_to_date(`your_date_column`,'%M')) BETWEEN month(str_to_date('January','%M')) 
                            AND   month(str_to_date('June','%M'))
于 2013-06-28T08:44:16.587 に答える