에 포함 된 날짜 테이블을 만듭니다 매일 신경 쓰는 행 (2000 년 1 월 1 일부터 2099 년 12 월 31 일까지) :
create table dates (the_date date primary key);
delimiter $$
create procedure populate_dates (p_start_date date, p_end_date date)
begin
declare v_date date;
set v_date = p_start_date;
while v_date <= p_end_date
do
insert ignore into dates (the_date) values (v_date);
set v_Date = date_add(v_date, interval 1 day);
end while;
end $$
delimiter ;
call populate_dates('2000-01-01','2099-12-31');
그런 다음 원하는 출력을 얻기 위해 다음과 같은 쿼리를 실행할 수 있습니다 월에 왼쪽 0 발생이 요일을 제외합니다
set @date = curdate();
select dayofweek(the_date) as dayOfWeek, count(*) as numLeft
from dates
where the_date >= @date
and the_date < str_to_date(period_add(date_format(@date,'%Y%m'),1),'%Y%m')
group by dayofweek(the_date);
합니다. 당신이 사람들을보고 싶은 경우 요일에 다른 테이블 (1-7) 만들 수 있습니다
create table days_of_week (
id tinyint unsigned not null primary key,
name char(10) not null
);
insert into days_of_week (id,name) values (1,'Sunday'),(2,'Monday'),
(3,'Tuesday'),(4,'Wednesday'),(5,'Thursday'),(6,'Friday'),(7,'Saturday');
을 그리고 왼쪽으로 해당 테이블을 조회하면 날짜 테이블에 조인
select w.id, count(d.the_Date) as numLeft
from days_of_week w
left outer join dates d on w.id = dayofweek(d.the_date)
and d.the_date >= @date
and d.the_date < str_to_date(period_add(date_format(@date,'%Y%m'),1),'%Y%m')
group by w.id;
다른 요일에도 적용하려면 심각한 수정이 필요하며 원하는 출력을 반환하는 버전을 만드는 것이 번거로울 수 있습니다. –
그것의 오늘 포함되지 않습니다 당신은 +1로 나누기 전에 +1을 추가 할 필요가 있다고 생각합니다. –
그리고 나는 이것을 하나의 쿼리에서 어떻게 수행 할 지 모릅니다. 아마도 함수에 넣고 호출 할 때 도움이 될 것입니다. 매개 변수에 '9' –