Is it possible to auto create querys according specific values?










0















Is it possible to create multiple querys choosing the day of week?



MYSQL query:



create table reservations (
id bigint(20) NOT NULL,
var_start datetime NOT NULL,
var_end datetime NOT NULL
)


For example



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”


Here is the html demo



I will appreciate some links or concepts to search the correct information about automatic querys or this kind of concepts.



Thanks in advance.










share|improve this question
























  • One possibility is using a number generator. Will the input variables be always one particular day, one particular month, one particular year and so on.. ?

    – Madhur Bhaiya
    Nov 16 '18 at 5:39











  • Yeah I understand that part; but at a time only one value will be input for these variables, right ?

    – Madhur Bhaiya
    Nov 16 '18 at 5:42











  • I believe the column1 is of date type, so the input should be in YYYY-MM-DD format instead.

    – Madhur Bhaiya
    Nov 16 '18 at 5:47











  • Please clarify the datatype of the columns in the table. It would be better if you add SHOW CREATE TABLE results to the question. I am working out a solution, and its complexity will vary on this.

    – Madhur Bhaiya
    Nov 16 '18 at 6:08






  • 1





    I am not talking about the JS side code. Have you already created the MySQL table ? What is its structure ? What are the datatypes defined on the MySQL table columns ?

    – Madhur Bhaiya
    Nov 16 '18 at 6:30















0















Is it possible to create multiple querys choosing the day of week?



MYSQL query:



create table reservations (
id bigint(20) NOT NULL,
var_start datetime NOT NULL,
var_end datetime NOT NULL
)


For example



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”


Here is the html demo



I will appreciate some links or concepts to search the correct information about automatic querys or this kind of concepts.



Thanks in advance.










share|improve this question
























  • One possibility is using a number generator. Will the input variables be always one particular day, one particular month, one particular year and so on.. ?

    – Madhur Bhaiya
    Nov 16 '18 at 5:39











  • Yeah I understand that part; but at a time only one value will be input for these variables, right ?

    – Madhur Bhaiya
    Nov 16 '18 at 5:42











  • I believe the column1 is of date type, so the input should be in YYYY-MM-DD format instead.

    – Madhur Bhaiya
    Nov 16 '18 at 5:47











  • Please clarify the datatype of the columns in the table. It would be better if you add SHOW CREATE TABLE results to the question. I am working out a solution, and its complexity will vary on this.

    – Madhur Bhaiya
    Nov 16 '18 at 6:08






  • 1





    I am not talking about the JS side code. Have you already created the MySQL table ? What is its structure ? What are the datatypes defined on the MySQL table columns ?

    – Madhur Bhaiya
    Nov 16 '18 at 6:30













0












0








0








Is it possible to create multiple querys choosing the day of week?



MYSQL query:



create table reservations (
id bigint(20) NOT NULL,
var_start datetime NOT NULL,
var_end datetime NOT NULL
)


For example



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”


Here is the html demo



I will appreciate some links or concepts to search the correct information about automatic querys or this kind of concepts.



Thanks in advance.










share|improve this question
















Is it possible to create multiple querys choosing the day of week?



MYSQL query:



create table reservations (
id bigint(20) NOT NULL,
var_start datetime NOT NULL,
var_end datetime NOT NULL
)


For example



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”


Here is the html demo



I will appreciate some links or concepts to search the correct information about automatic querys or this kind of concepts.



Thanks in advance.







mysql database phpmyadmin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 19:47







Ezio Auditore

















asked Nov 16 '18 at 5:33









Ezio AuditoreEzio Auditore

1078




1078












  • One possibility is using a number generator. Will the input variables be always one particular day, one particular month, one particular year and so on.. ?

    – Madhur Bhaiya
    Nov 16 '18 at 5:39











  • Yeah I understand that part; but at a time only one value will be input for these variables, right ?

    – Madhur Bhaiya
    Nov 16 '18 at 5:42











  • I believe the column1 is of date type, so the input should be in YYYY-MM-DD format instead.

    – Madhur Bhaiya
    Nov 16 '18 at 5:47











  • Please clarify the datatype of the columns in the table. It would be better if you add SHOW CREATE TABLE results to the question. I am working out a solution, and its complexity will vary on this.

    – Madhur Bhaiya
    Nov 16 '18 at 6:08






  • 1





    I am not talking about the JS side code. Have you already created the MySQL table ? What is its structure ? What are the datatypes defined on the MySQL table columns ?

    – Madhur Bhaiya
    Nov 16 '18 at 6:30

















  • One possibility is using a number generator. Will the input variables be always one particular day, one particular month, one particular year and so on.. ?

    – Madhur Bhaiya
    Nov 16 '18 at 5:39











  • Yeah I understand that part; but at a time only one value will be input for these variables, right ?

    – Madhur Bhaiya
    Nov 16 '18 at 5:42











  • I believe the column1 is of date type, so the input should be in YYYY-MM-DD format instead.

    – Madhur Bhaiya
    Nov 16 '18 at 5:47











  • Please clarify the datatype of the columns in the table. It would be better if you add SHOW CREATE TABLE results to the question. I am working out a solution, and its complexity will vary on this.

    – Madhur Bhaiya
    Nov 16 '18 at 6:08






  • 1





    I am not talking about the JS side code. Have you already created the MySQL table ? What is its structure ? What are the datatypes defined on the MySQL table columns ?

    – Madhur Bhaiya
    Nov 16 '18 at 6:30
















One possibility is using a number generator. Will the input variables be always one particular day, one particular month, one particular year and so on.. ?

– Madhur Bhaiya
Nov 16 '18 at 5:39





One possibility is using a number generator. Will the input variables be always one particular day, one particular month, one particular year and so on.. ?

– Madhur Bhaiya
Nov 16 '18 at 5:39













Yeah I understand that part; but at a time only one value will be input for these variables, right ?

– Madhur Bhaiya
Nov 16 '18 at 5:42





Yeah I understand that part; but at a time only one value will be input for these variables, right ?

– Madhur Bhaiya
Nov 16 '18 at 5:42













I believe the column1 is of date type, so the input should be in YYYY-MM-DD format instead.

– Madhur Bhaiya
Nov 16 '18 at 5:47





I believe the column1 is of date type, so the input should be in YYYY-MM-DD format instead.

– Madhur Bhaiya
Nov 16 '18 at 5:47













Please clarify the datatype of the columns in the table. It would be better if you add SHOW CREATE TABLE results to the question. I am working out a solution, and its complexity will vary on this.

– Madhur Bhaiya
Nov 16 '18 at 6:08





Please clarify the datatype of the columns in the table. It would be better if you add SHOW CREATE TABLE results to the question. I am working out a solution, and its complexity will vary on this.

– Madhur Bhaiya
Nov 16 '18 at 6:08




1




1





I am not talking about the JS side code. Have you already created the MySQL table ? What is its structure ? What are the datatypes defined on the MySQL table columns ?

– Madhur Bhaiya
Nov 16 '18 at 6:30





I am not talking about the JS side code. Have you already created the MySQL table ? What is its structure ? What are the datatypes defined on the MySQL table columns ?

– Madhur Bhaiya
Nov 16 '18 at 6:30












1 Answer
1






active

oldest

votes


















4














We will be basically dynamically generating all the required dates within the query itself. And then use that result-set to Insert into the reservations table.



I have changed id column to Primary Key and Auto Increment (as it should be).



In a Derived table, we will use a number generator from 0 to 4, as there can be at maximum 5 Wednesdays (and other weekdays) in a month.



Now we will try to get the first Sunday of the required month. For that, we will firstly create a date corresponding to first date of the month, using the input variable values for the month and year:



STR_TO_DATE(CONCAT('2018','11','01'), '%Y%c%d')


Concat('2018','11','01') basically generates 20181101 string. We can then use Str_to_date() function to convert it into MySQL date format. We could have use Concat() function directly to get in YYYY-MM-DD format; but this approach should be robust in case the input month is 9 instead of 09.



Now, we will use various Datetime functions to determine the nth Wednesday. I have expanded over the answer originally given here: https://stackoverflow.com/a/13405764/2469308



Number generator table will help us in calculating the 1st, 2nd, 3rd Wednesday and so on.. We can basically get the first Wednesday by adding 3 number of days to the first Sunday. Afterwards, we will basically add 7 days everytime to get next Wednesday in the month.



Eventually, we will use all these dates and AddTime() to them for determining var_start and var_end accordingly. Also, there is a possibility that in the 5th day, it may cross to next month. So we will filter those out using WHERE MONTH(..) .. AND YEAR(..) .. conditions.



Finally, INSERT INTO.. SELECT statement will be used to insert into the reservations table.



Schema (MySQL v5.7) View on DB Fiddle



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
*/



Query #1



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;



Query #2



SELECT * FROM reservations;

| id | var_start | var_end |
| --- | ------------------- | ------------------- |
| 1 | 2018-11-07 11:00:00 | 2018-11-07 13:00:00 |
| 2 | 2018-11-14 11:00:00 | 2018-11-14 13:00:00 |
| 3 | 2018-11-21 11:00:00 | 2018-11-21 13:00:00 |
| 4 | 2018-11-28 11:00:00 | 2018-11-28 13:00:00 |



In terms of input variables (prefixed with : for parametric queries), the query would looks as follows:



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;





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332000%2fis-it-possible-to-auto-create-querys-according-specific-values%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    We will be basically dynamically generating all the required dates within the query itself. And then use that result-set to Insert into the reservations table.



    I have changed id column to Primary Key and Auto Increment (as it should be).



    In a Derived table, we will use a number generator from 0 to 4, as there can be at maximum 5 Wednesdays (and other weekdays) in a month.



    Now we will try to get the first Sunday of the required month. For that, we will firstly create a date corresponding to first date of the month, using the input variable values for the month and year:



    STR_TO_DATE(CONCAT('2018','11','01'), '%Y%c%d')


    Concat('2018','11','01') basically generates 20181101 string. We can then use Str_to_date() function to convert it into MySQL date format. We could have use Concat() function directly to get in YYYY-MM-DD format; but this approach should be robust in case the input month is 9 instead of 09.



    Now, we will use various Datetime functions to determine the nth Wednesday. I have expanded over the answer originally given here: https://stackoverflow.com/a/13405764/2469308



    Number generator table will help us in calculating the 1st, 2nd, 3rd Wednesday and so on.. We can basically get the first Wednesday by adding 3 number of days to the first Sunday. Afterwards, we will basically add 7 days everytime to get next Wednesday in the month.



    Eventually, we will use all these dates and AddTime() to them for determining var_start and var_end accordingly. Also, there is a possibility that in the 5th day, it may cross to next month. So we will filter those out using WHERE MONTH(..) .. AND YEAR(..) .. conditions.



    Finally, INSERT INTO.. SELECT statement will be used to insert into the reservations table.



    Schema (MySQL v5.7) View on DB Fiddle



    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
    */



    Query #1



    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;



    Query #2



    SELECT * FROM reservations;

    | id | var_start | var_end |
    | --- | ------------------- | ------------------- |
    | 1 | 2018-11-07 11:00:00 | 2018-11-07 13:00:00 |
    | 2 | 2018-11-14 11:00:00 | 2018-11-14 13:00:00 |
    | 3 | 2018-11-21 11:00:00 | 2018-11-21 13:00:00 |
    | 4 | 2018-11-28 11:00:00 | 2018-11-28 13:00:00 |



    In terms of input variables (prefixed with : for parametric queries), the query would looks as follows:



    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;





    share|improve this answer





























      4














      We will be basically dynamically generating all the required dates within the query itself. And then use that result-set to Insert into the reservations table.



      I have changed id column to Primary Key and Auto Increment (as it should be).



      In a Derived table, we will use a number generator from 0 to 4, as there can be at maximum 5 Wednesdays (and other weekdays) in a month.



      Now we will try to get the first Sunday of the required month. For that, we will firstly create a date corresponding to first date of the month, using the input variable values for the month and year:



      STR_TO_DATE(CONCAT('2018','11','01'), '%Y%c%d')


      Concat('2018','11','01') basically generates 20181101 string. We can then use Str_to_date() function to convert it into MySQL date format. We could have use Concat() function directly to get in YYYY-MM-DD format; but this approach should be robust in case the input month is 9 instead of 09.



      Now, we will use various Datetime functions to determine the nth Wednesday. I have expanded over the answer originally given here: https://stackoverflow.com/a/13405764/2469308



      Number generator table will help us in calculating the 1st, 2nd, 3rd Wednesday and so on.. We can basically get the first Wednesday by adding 3 number of days to the first Sunday. Afterwards, we will basically add 7 days everytime to get next Wednesday in the month.



      Eventually, we will use all these dates and AddTime() to them for determining var_start and var_end accordingly. Also, there is a possibility that in the 5th day, it may cross to next month. So we will filter those out using WHERE MONTH(..) .. AND YEAR(..) .. conditions.



      Finally, INSERT INTO.. SELECT statement will be used to insert into the reservations table.



      Schema (MySQL v5.7) View on DB Fiddle



      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
      */



      Query #1



      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;



      Query #2



      SELECT * FROM reservations;

      | id | var_start | var_end |
      | --- | ------------------- | ------------------- |
      | 1 | 2018-11-07 11:00:00 | 2018-11-07 13:00:00 |
      | 2 | 2018-11-14 11:00:00 | 2018-11-14 13:00:00 |
      | 3 | 2018-11-21 11:00:00 | 2018-11-21 13:00:00 |
      | 4 | 2018-11-28 11:00:00 | 2018-11-28 13:00:00 |



      In terms of input variables (prefixed with : for parametric queries), the query would looks as follows:



      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;





      share|improve this answer



























        4












        4








        4







        We will be basically dynamically generating all the required dates within the query itself. And then use that result-set to Insert into the reservations table.



        I have changed id column to Primary Key and Auto Increment (as it should be).



        In a Derived table, we will use a number generator from 0 to 4, as there can be at maximum 5 Wednesdays (and other weekdays) in a month.



        Now we will try to get the first Sunday of the required month. For that, we will firstly create a date corresponding to first date of the month, using the input variable values for the month and year:



        STR_TO_DATE(CONCAT('2018','11','01'), '%Y%c%d')


        Concat('2018','11','01') basically generates 20181101 string. We can then use Str_to_date() function to convert it into MySQL date format. We could have use Concat() function directly to get in YYYY-MM-DD format; but this approach should be robust in case the input month is 9 instead of 09.



        Now, we will use various Datetime functions to determine the nth Wednesday. I have expanded over the answer originally given here: https://stackoverflow.com/a/13405764/2469308



        Number generator table will help us in calculating the 1st, 2nd, 3rd Wednesday and so on.. We can basically get the first Wednesday by adding 3 number of days to the first Sunday. Afterwards, we will basically add 7 days everytime to get next Wednesday in the month.



        Eventually, we will use all these dates and AddTime() to them for determining var_start and var_end accordingly. Also, there is a possibility that in the 5th day, it may cross to next month. So we will filter those out using WHERE MONTH(..) .. AND YEAR(..) .. conditions.



        Finally, INSERT INTO.. SELECT statement will be used to insert into the reservations table.



        Schema (MySQL v5.7) View on DB Fiddle



        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
        */



        Query #1



        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;



        Query #2



        SELECT * FROM reservations;

        | id | var_start | var_end |
        | --- | ------------------- | ------------------- |
        | 1 | 2018-11-07 11:00:00 | 2018-11-07 13:00:00 |
        | 2 | 2018-11-14 11:00:00 | 2018-11-14 13:00:00 |
        | 3 | 2018-11-21 11:00:00 | 2018-11-21 13:00:00 |
        | 4 | 2018-11-28 11:00:00 | 2018-11-28 13:00:00 |



        In terms of input variables (prefixed with : for parametric queries), the query would looks as follows:



        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;





        share|improve this answer















        We will be basically dynamically generating all the required dates within the query itself. And then use that result-set to Insert into the reservations table.



        I have changed id column to Primary Key and Auto Increment (as it should be).



        In a Derived table, we will use a number generator from 0 to 4, as there can be at maximum 5 Wednesdays (and other weekdays) in a month.



        Now we will try to get the first Sunday of the required month. For that, we will firstly create a date corresponding to first date of the month, using the input variable values for the month and year:



        STR_TO_DATE(CONCAT('2018','11','01'), '%Y%c%d')


        Concat('2018','11','01') basically generates 20181101 string. We can then use Str_to_date() function to convert it into MySQL date format. We could have use Concat() function directly to get in YYYY-MM-DD format; but this approach should be robust in case the input month is 9 instead of 09.



        Now, we will use various Datetime functions to determine the nth Wednesday. I have expanded over the answer originally given here: https://stackoverflow.com/a/13405764/2469308



        Number generator table will help us in calculating the 1st, 2nd, 3rd Wednesday and so on.. We can basically get the first Wednesday by adding 3 number of days to the first Sunday. Afterwards, we will basically add 7 days everytime to get next Wednesday in the month.



        Eventually, we will use all these dates and AddTime() to them for determining var_start and var_end accordingly. Also, there is a possibility that in the 5th day, it may cross to next month. So we will filter those out using WHERE MONTH(..) .. AND YEAR(..) .. conditions.



        Finally, INSERT INTO.. SELECT statement will be used to insert into the reservations table.



        Schema (MySQL v5.7) View on DB Fiddle



        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
        */



        Query #1



        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;



        Query #2



        SELECT * FROM reservations;

        | id | var_start | var_end |
        | --- | ------------------- | ------------------- |
        | 1 | 2018-11-07 11:00:00 | 2018-11-07 13:00:00 |
        | 2 | 2018-11-14 11:00:00 | 2018-11-14 13:00:00 |
        | 3 | 2018-11-21 11:00:00 | 2018-11-21 13:00:00 |
        | 4 | 2018-11-28 11:00:00 | 2018-11-28 13:00:00 |



        In terms of input variables (prefixed with : for parametric queries), the query would looks as follows:



        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;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 18 '18 at 18:39

























        answered Nov 18 '18 at 18:17









        Madhur BhaiyaMadhur Bhaiya

        19.6k62236




        19.6k62236





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332000%2fis-it-possible-to-auto-create-querys-according-specific-values%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党