SQL 根据 table 中的值插入 table

SQL insert into a table depending on values in the table

我正在创建一个每本书只有一本的图书馆系统。用户将输入这本书和他们想要的日期。之后系统将检查该书是否未在用户想要的日期保留。

如果 table 中没有变量,我正在尝试将数据插入到 table 中。例如如果 id 等于 3 并且日期介于 table 中已有的两个日期之间,则不会输入信息。 table 用于简单的图书预订系统。下面的代码是我所拥有的,但似乎不起作用,因此需要更改。我得到的错误在代码下方。谢谢。

SELECT * 
FROM `table_name` 
INSERT INTO table_name (name, id, start_date, end_date, days)
VALUES ('test', '4', '0000-00-00', '1000-00-00', 3)
WHERE id != 3
AND start_date NOT BETWEEN 2016-03-31 AND 2016-03-05

这是我在 运行 代码时遇到的错误:

 Static analysis:

9 errors were found during analysis.

Unrecognized keyword. (near "NOT" at position 203)
Unrecognized keyword. (near "BETWEEN" at position 207)
Unexpected token. (near "2016" at position 215)
Unexpected token. (near "-03" at position 219)
Unexpected token. (near "-31" at position 222)
Unrecognized keyword. (near "AND" at position 226)
Unexpected token. (near "2016" at position 230)
Unexpected token. (near "-03" at position 234)
Unexpected token. (near "-05" at position 237)
SQL query: Documentation

SELECT * FROM `table_name` INSERT LIMIT 0, 25 INTO table_name (name, id, start_date, end_date, days) VALUES ('test', '4', '0000-00-00', '1000-00-00', 3) WHERE id != 3 AND start_date NOT BETWEEN 2016-03-31 AND 2016-03-05

MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT LIMIT 0, 25 INTO table_name (name, id, start_date, end_date, days)' at line 3#

您可能想要这样的东西:

INSERT INTO table_name (name, id, start_date, end_date, days)
SELECT name, id, start_date, end_date, days
FROM (SELECT 'test' AS name, 
             4 AS id, 
             '0000-00-00' AS start_date, 
             '1000-00-00' AS  end_date, 
             3 AS days) AS t
WHERE NOT EXISTS (SELECT *
                  FROM table_name
                  WHERE id = 3 AND start_date BETWEEN '2016-03-31' AND '2016-03-05')

此查询将在 table_name if 中插入指定的 硬编码 id=3 start_date之间的日期('2016-03-31', '2016-03-05')不存在在同一个table。

您可以根据实际需要修改WHERE子句的谓词。

  1. 您正在插入与 select 相同的 table 数据。是你想要的吗?

  2. 是否只想在table中插入VALUES ('test', '4', '0000-00-00', '1000-00-00', 3)?在那种情况下,为什么要使用 where 子句。

  3. 如果你想根据你想要的条件从你的 table 中 select 数据,那你为什么要用它来凑 VALUES ('test', '4', '0000-00-00', '1000-00-00', 3)?您需要单独的插入语句。

  4. 用于between子句的值应该左边小右边大。你也应该理想地对其进行类型转换。 start_date NOT BETWEEN STR_TO_DATE('2016-03-05','%Y-%m-%d') and STR_TO_DATE('2016-03-31','%Y-%m-%d')

  5. 最后但同样重要的是,insert 子句出现在 select.

  6. 之前

请回答问题 and/or 根据这些输入更改您的查询。

编辑:根据 OP 注释,生成了 2 个插入语句。您可以使用 dual 中的 union 和 select 值,而不是使用 statement2,但我不确定 mysql 中那种情况下的日期类型转换。

Statement1:

INSERT INTO table_name (name, id, start_date, end_date, days)
SELECT * 
FROM `table_name` WHERE id != 3
AND start_date NOT BETWEEN STR_TO_DATE('2016-03-05','%Y-%m-%d')  and STR_TO_DATE('2016-03-31','%Y-%m-%d')

Statement2

insert into table_name (name, id, start_date, end_date, days)
values ('test', '4', '0000-00-00', '1000-00-00', 3)

试试这个

SELECT * FROM `table_name` (INSERT LIMIT 0, 25 INTO table_name 
(name, id, start_date, end_date, days) 
VALUES ('test', '4', '0000-00-00', '1000-00-00', 3))
WHERE id != 3 AND start_date NOT BETWEEN '2016-03-31' AND '2016-03-05'

事实证明,我只需要在代码的不同部分分别执行这两件事。