简体   繁体   English

选择余额从今天开始大于10000并且到期日期小于2天的行

[英]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] [ID] [所有者] [余额] [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. 首先,我不知道'ExpirationDate时间戳DATETIME NOT NULL DEFAULT GETDATE'是否遵守有关日期格式的mysql语法。 Is it yyyy/mm/dd or mm/dd/yyyy? 是yyyy / mm / dd还是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. 我必须“从今天开始选择余额大于1000并且到期日期小于2天的行。

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 我想从CURRENT_DATE算起日期,无论何时,例如

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. 如前所述,您的DDL不正确。

Here is corrected DDL for WebSQL/SQLLite and a query for you to study. 这是针对WebSQL / SQLLite的更正DDL,以及供您学习的查询。

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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