简体   繁体   中英

Select the rows where Balance is greater than 10000 and expiration date is less than 2 days from today

I have the following table:

[ID] [Owner] [Balance] [CreationDate] [ExpirationDate]

The code is as follows so far.

CREATE TABLE Account(ID INTEGER PRIMARY KEY AUTOINCREMENT,
                         Owner varchar(50),
                         Balance decimal(7,3),
                         CreationDate timestamp DEFAULT CURRENT_TIMESTAMP,
                         ExpirationDate timestamp DATETIME NOT NULL DEFAULT CURDATE)

    INSERT INTO Account(Owner, Balance, ExpirationDate)
                Values('Jean Claude Van Damme', 6453, '2023-10-06'),
                ('Peter Paul Rubens', 9999, '2018-06-04'),
                ('Rembrandt Harmenszoon van Rijn', 765549, '2023-10-22'),
                ('Pablo Ruiz y Picasso', 972825, '2019-06-13'),
                ('Michelangelo di Lodovico Buonarroti', 7981, '2022-10-27'),
                ('Raffaello Sanzio da Urbino', 548765, '2018-11-17'),
                ('John Doe', 666745, '2021-12-05'),
                ('Mickey Mouse', 10005, '2020-10-21'),
                ('Vincent Willem van Gogh', 453906, '2021-07-13'),
                ('Oliver Hardy', 1005, '2019-01-26')


    SELECT * FROM Account

    SELECT Owner FROM Account WHERE Balance < 10000

First I don't know if the 'ExpirationDate timestamp DATETIME NOT NULL DEFAULT GETDATE' respects the mysql syntax regarding date formats. Is it yyyy/mm/dd or mm/dd/yyyy? I know the date can be converted to respect the European standard, but that doesn't interest me.

Second. I have to "Select the rows where Balance is greater than 1000 and expiration date is less than 2 days from today.

Obviously I would take the easy way out and add to my code

SELECT Owner FROM Account
WHERE Balance >= 10000 AND ExpirationDate BETWEEN '2017-04-20' AND '2017-04-22'

BUT

I'd like to have the date computed from CURRENT_DATE regardless of when, something like

SELECT Owner FROM Account
WHERE Balance >= 10000 AND ExpirationDate BETWEEN CURDATE() AND INTERVAL 2 DAY)

I've also tried

SELECT Owner FROM Account
WHERE Balance >= 10000 AND ExpirationDate = DATE_ADD(CURDATE(), INTERVAL 2 DAY);

SELECT Owner FROM Account
WHERE Balance >= 10000 AND DATE_SUB(GETDATE(), INTERVAL 10 DAY);

Needless to say either ways don't work.

As noted, your DDL was incorrect.

Here is corrected DDL for WebSQL/SQLLite and a query for you to study.

CREATE TABLE Account(
    ID INTEGER PRIMARY KEY,
    Owner varchar(50),
    Balance decimal(10,3),
    CreationDate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    ExpirationDate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO Account(Owner, Balance, ExpirationDate) Values
    ('Jean Claude Van Damme', 6453, '2023-10-06'),
    ('Peter Paul Rubens', 9999, '2018-06-04'),
    ('Rembrandt Harmenszoon van Rijn', 765549, '2023-10-22'),
    ('Pablo Ruiz y Picasso', 972825, '2019-06-13'),
    ('Michelangelo di Lodovico Buonarroti', 7981, '2022-10-27'),
    ('Raffaello Sanzio da Urbino', 548765, '2018-11-17'),
    ('John Doe', 666745, '2021-12-05'),
    ('Mickey Mouse', 10005, '2020-10-21'),
    ('Vincent Willem van Gogh', 453906, '2021-07-13'),
    ('Oliver Hardy', 1005, '2019-01-26');

SELECT * FROM Account
WHERE Balance >= 10000
AND ExpirationDate > DATE()
AND ExpirationDate < DATE('now','+2 day')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM