current_timestamp interval 4 day in into 或其他表达式

current_timestamp interval 4 day in into or other expression

你好,我有这段代码,我想在 table 中添加数据,也是当天 +4 天的间隔,我尝试使用 table 创建的 beginig 没有工作,或者我需要一个将日期值更改为 current_timestamp 间隔 4 天的表达式。

CREATE TABLE IF NOT EXISTS `Spital`.`Stoc General` (
  `ID Produs` INT ,
  `Denumire` VARCHAR(45) NOT NULL,
  `Cantitate` INT NULL,
  PRIMARY KEY (`ID Produs`))
ENGINE = InnoDB;

Select*From `Spital`.`Stoc General`;
INSERT INTO `spital`.`stoc general` (`ID Produs`, `Denumire`, `Cantitate`) VALUES ('1', 'Clabax', '20');
INSERT INTO `spital`.`stoc general` (`ID Produs`, `Denumire`, `Cantitate`) VALUES ('2', 'Betadina', '15');
INSERT INTO `spital`.`stoc general` (`ID Produs`, `Denumire`, `Cantitate`) VALUES ('3', 'Paracetamo', '4');
INSERT INTO `spital`.`stoc general` (`ID Produs`, `Denumire`, `Cantitate`) VALUES ('4', 'Oxigen', '3');


CREATE TABLE if not exists `Spital`.`Stoc URGENT` (
  `ID Produs U` INT NOT NULL AUTO_INCREMENT,
`id produs` int not null,
  `Denumire` VARCHAR(45) NOT NULL,
  `Cantitate` INT NOT NULL,
   `Data livrari` DATETIME On update CURRENT_TIMESTAMP(),
  PRIMARY KEY (`ID Produs U`))
ENGINE = MEMORY;

INSERT INTO `spital`.`stoc urgent`
            (`id produs`,
             `denumire`,
             `cantitate`)
SELECT `id produs`,
       `denumire`,
       `cantitate`
FROM   `spital`.`stoc general` 
where  `cantitate`<'5';

插入时需要设置值,所以必须设置DEFAULT列的选项:

CREATE TABLE if not exists `Spital`.`Stoc URGENT` (
  `ID Produs U` INT NOT NULL AUTO_INCREMENT,
`id produs` int not null,
  `Denumire` VARCHAR(45) NOT NULL,
  `Cantitate` INT NOT NULL,
   `Data livrari` DATETIME DEFAULT (CURRENT_TIMESTAMP + INTERVAL 4 DAY)
                           On update CURRENT_TIMESTAMP(),
  PRIMARY KEY (`ID Produs U`))
ENGINE = MEMORY;
  1. 表达式两边的括号是强制性的。
  2. 您不能在 ON UPDATE 选项中设置相同的表达式。

我建议将 StockUrgent 创建为视图比创建新视图更好 table。存储重复值没有意义。
我还冒昧地删除了 table 和列名中的空格。这避免了用反引号包围所有名称的需要,并加快了编写查询的速度。

CREATE TABLE IF NOT EXISTS StockGeneral (
IdProdus INT ,
  Denumire VARCHAR(45) NOT NULL,
  Cantitate DATE NOT NULL,
  PRIMARY KEY (IdProdus));
INSERT INTO StockGeneral  VALUES ('1', 'Clabax', '2022-03-20');
INSERT INTO StockGeneral  VALUES ('2', 'Betadina', '2022-03-15');
INSERT INTO  StockGeneral  VALUES ('3', 'Paracetamo', '2022-03-24');
INSERT INTO  StockGeneral  VALUES ('4', 'Oxigen', '2022-03-30');
SELECT 
  idProdus,
  Denumire,
  cantitate,
  DateDiff(cantitate,current_date())
FROM StockGeneral
idProdus | Denumire   | cantitate  | DateDiff(cantitate,current_date())
-------: | :--------- | :--------- | ---------------------------------:
       1 | Clabax     | 2022-03-20 |                                  4
       2 | Betadina   | 2022-03-15 |                                 -1
       3 | Paracetamo | 2022-03-24 |                                  8
       4 | Oxigen     | 2022-03-30 |                                 14
CREATE VIEW StockUrgent AS
SELECT 
  idProdus,
  Denumire,
  cantitate
FROM StockGeneral
WHERE DateDiff(cantitate,current_date())< 5;
SELECT * FROM StockUrgent;
idProdus | Denumire | cantitate 
-------: | :------- | :---------
       1 | Clabax   | 2022-03-20
       2 | Betadina | 2022-03-15

db<>fiddle here