SQL根据表中的值插入表中

时间:2016-03-08 10:23:46

标签: mysql sql select insert-into

我正在创建一个只有每本书一份的图书馆系统。用户可以输入书籍和他们想要的日期。在系统检查到该书没有为用户想要的日期保留之后。

如果变量不在表中,我试图将数据插入表中。例如如果id等于3且日期在表中已有的两个日期之间,则不会输入信息。该表用于简单的预订系统。下面的代码就是我所拥有的,但似乎没有用,所以需要改变。我得到的错误是在代码下面。谢谢。

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#

4 个答案:

答案 0 :(得分:1)

你可能想要这样的东西:

Intent intent = new Intent(AEScreenOnOffService.this,postlockscreen.class);
            startActivity(intent);



            } else {

                // your code here
                // Some time required to stop any service to save battery consumption
                //Toast.makeText(getBaseContext(), "Screen off,", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }

}

此查询将在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 {{ 1}}在同一个表中的日期id=3 不存在之间。

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

答案 1 :(得分:0)

  1. 您正在选择同一表格中的数据。这是你想要的吗?

  2. 您是否只想在表格中插入VALUES ('test', '4', '0000-00-00', '1000-00-00', 3)?在这种情况下,为什么where条款。

  3. 如果您想根据自己想要的条件从表格中选择数据,那么为什么要将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. 请根据这些输入回答问题和/或更改您的查询。

    编辑:基于OP注释,生成2个插入语句。您可以使用statement2而不是使用union,并从dual中选择值,但我不确定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)
    

答案 2 :(得分:0)

试试这个

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'

答案 3 :(得分:0)

事实证明,我需要在代码的不同部分单独完成这两件事。