MySQL で:
CREATE TABLE calendar
(
date date not null,
isBus bit
);
ALTER TABLE calendar
ADD CONSTRAINT PK_calendar
PRIMARY KEY(date);
INSERT INTO calendar VALUES ('2012-10-01', 1);
INSERT INTO calendar VALUES ('2012-10-02', 1);
INSERT INTO calendar VALUES ('2012-10-03', 1);
INSERT INTO calendar VALUES ('2012-10-04', 1);
INSERT INTO calendar VALUES ('2012-10-05', 1);
INSERT INTO calendar VALUES ('2012-10-06', 0);
INSERT INTO calendar VALUES ('2012-10-07', 0);
INSERT INTO calendar VALUES ('2012-10-08', 1);
INSERT INTO calendar VALUES ('2012-10-09', 1);
INSERT INTO calendar VALUES ('2012-10-10', 1);
INSERT INTO calendar VALUES ('2012-10-11', 1);
INSERT INTO calendar VALUES ('2012-10-12', 1);
INSERT INTO calendar VALUES ('2012-10-13', 0);
INSERT INTO calendar VALUES ('2012-10-14', 0);
INSERT INTO calendar VALUES ('2012-10-15', 1);
INSERT INTO calendar VALUES ('2012-10-16', 1);
INSERT INTO calendar VALUES ('2012-10-17', 1);
INSERT INTO calendar VALUES ('2012-10-18', 1);
INSERT INTO calendar VALUES ('2012-10-19', 1);
INSERT INTO calendar VALUES ('2012-10-20', 0);
INSERT INTO calendar VALUES ('2012-10-21', 0);
INSERT INTO calendar VALUES ('2012-10-22', 1);
INSERT INTO calendar VALUES ('2012-10-23', 1);
INSERT INTO calendar VALUES ('2012-10-24', 1);
INSERT INTO calendar VALUES ('2012-10-25', 1);
INSERT INTO calendar VALUES ('2012-10-26', 1);
INSERT INTO calendar VALUES ('2012-10-27', 0);
INSERT INTO calendar VALUES ('2012-10-28', 0);
INSERT INTO calendar VALUES ('2012-10-29', 1);
INSERT INTO calendar VALUES ('2012-10-30', 1);
INSERT INTO calendar VALUES ('2012-10-31', 1);
INSERT INTO calendar VALUES ('2012-11-01', 1);
INSERT INTO calendar VALUES ('2012-11-02', 1);
INSERT INTO calendar VALUES ('2012-11-03', 0);
INSERT INTO calendar VALUES ('2012-11-04', 0);
INSERT INTO calendar VALUES ('2012-11-05', 1);
INSERT INTO calendar VALUES ('2012-11-06', 1);
INSERT INTO calendar VALUES ('2012-11-07', 1);
INSERT INTO calendar VALUES ('2012-11-08', 1);
INSERT INTO calendar VALUES ('2012-11-09', 1);
INSERT INTO calendar VALUES ('2012-11-10', 0);
INSERT INTO calendar VALUES ('2012-11-11', 0);
INSERT INTO calendar VALUES ('2012-11-12', 1);
INSERT INTO calendar VALUES ('2012-11-13', 1);
INSERT INTO calendar VALUES ('2012-11-14', 1);
INSERT INTO calendar VALUES ('2012-11-15', 1);
INSERT INTO calendar VALUES ('2012-11-16', 1);
INSERT INTO calendar VALUES ('2012-11-17', 0);
INSERT INTO calendar VALUES ('2012-11-18', 0);
INSERT INTO calendar VALUES ('2012-11-19', 1);
INSERT INTO calendar VALUES ('2012-11-20', 1);
INSERT INTO calendar VALUES ('2012-11-21', 1);
INSERT INTO calendar VALUES ('2012-11-22', 1);
INSERT INTO calendar VALUES ('2012-11-23', 1);
INSERT INTO calendar VALUES ('2012-11-24', 0);
INSERT INTO calendar VALUES ('2012-11-25', 0);
INSERT INTO calendar VALUES ('2012-11-26', 1);
INSERT INTO calendar VALUES ('2012-11-27', 1);
INSERT INTO calendar VALUES ('2012-11-28', 1);
INSERT INTO calendar VALUES ('2012-11-29', 1);
INSERT INTO calendar VALUES ('2012-11-30', 1);
次に、10 月のすべての木曜日 (5) を取得するには:
select @rowid:=@rowid+1, calendar.* from calendar, (SELECT @rowid:=0) as init
where dayofweek(date) = 5
and month(date) = 10
order by date;
10 月の第 2 木曜日を取得するには:
select * from
(select @rowid:=@rowid+1 as seqNo, calendar.* from calendar, (SELECT @rowid:=0) as init
where dayofweek(date) = 5
and month(date) = 10
order by date) x
where seqNo = 2;