可以创建多个查询来选择星期几吗?
mysql查询:
create table reservations (
id bigint(20) NOT NULL,
var_start datetime NOT NULL,
var_end datetime NOT NULL
)
例如
var_day = "3" // Wednesday
var_month = "11" // November
var_year = "2018"
var_start = "11:00” // 11 am
var_end = "13:00” // 1 pm
//This will create all inserts according all wednesday on november 2018.
insert into table var_start = "07-11-2018 11:00:00" var_end = "07-11-2018 13:00:00"
insert into table var_start = "14-11-2018 11:00:00" var_end = "07-11-2018 13:00:00”
insert into table var_start = "21-11-2018 11:00:00" var_end = "07-11-2018 13:00:00”
insert into table var_start = "28-11-2018 11:00:00" var_end = "07-11-2018 13:00:00”
这是html演示
我将感谢一些链接或概念,以搜索有关自动查询或这类概念的正确信息。
提前谢谢。
1条答案
按热度按时间wgx48brx1#
我们将基本上在查询本身中动态生成所有必需的日期。然后使用该结果集插入
reservations
table。我变了
id
列到主键并自动递增(按原样)。在派生表中,我们将使用从0到4的数字生成器,因为一个月最多可以有5个星期三(和其他工作日)。
现在我们将尝试获取所需月份的第一个星期日。为此,我们将首先使用月份和年份的输入变量值创建与月份的第一个日期相对应的日期:
create table reservations (
id bigint(20) NOT NULL PRIMARY KEY AUTO_INCREMENT,
var_start datetime NOT NULL,
var_end datetime NOT NULL
);
/*
var_day = "3" // Wednesday
var_month = "11" // November
var_year = "2018"
var_start = "11:00” // 11 am
var_end = "13:00” // 1 pm
INSERT INTO reservations (var_start, var_end)
SELECT
ADDTIME(dates.nth_date, '11:00') AS var_start,
ADDTIME(dates.nth_date, '13:00') AS var_end
FROM
(
SELECT
STR_TO_DATE(CONCAT('2018','11','01'), '%Y%c%d') +
INTERVAL (6 -
WEEKDAY(STR_TO_DATE(CONCAT('2018','11','01'), '%Y%c%d')) +
3 +
(7*nth)) DAY AS nth_date
FROM
(SELECT 0 AS nth UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4) AS num_gen
) AS dates
WHERE MONTH(dates.nth_date) = 11 AND
YEAR(dates.nth_date) = 2018;
SELECT * FROM reservations;
INSERT INTO reservations (var_start, var_end)
SELECT
ADDTIME(dates.nth_date, :var_start) AS var_start,
ADDTIME(dates.nth_date, :var_end) AS var_end
FROM
(
SELECT
STR_TO_DATE(CONCAT(:var_year,:var_month,'01'), '%Y%c%d') +
INTERVAL (6 -
WEEKDAY(STR_TO_DATE(CONCAT(:var_year,:var_month,'01'), '%Y%c%d')) +
:var_day +
(7*nth)) DAY AS nth_date
FROM
(SELECT 0 AS nth UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4) AS num_gen
) AS dates
WHERE MONTH(dates.nth_date) = :var_month AND
YEAR(dates.nth_date) = :var_year;