MySQL Insert Where query

前端 未结 27 1804
悲&欢浪女
悲&欢浪女 2020-11-22 06:16

What\'s wrong with this query:

INSERT INTO Users( weight, desiredWeight ) VALUES ( 160, 145 ) WHERE id = 1;

It works without the WHE

相关标签:
27条回答
  • 2020-11-22 06:57

    The simplest way is to use IF to violate your a key constraint. This only works for INSERT IGNORE but will allow you to use constraint in a INSERT.

    INSERT INTO Test (id, name) VALUES (IF(1!=0,NULL,1),'Test');
    
    0 讨论(0)
  • 2020-11-22 06:57

    I think your best option is use REPLACE instead INSERT

    REPLACE INTO Users(id, weight, desiredWeight) VALUES(1, 160, 145);

    0 讨论(0)
  • 2020-11-22 06:58

    I do not believe the insert has a WHERE clause.

    0 讨论(0)
  • 2020-11-22 06:58

    Insert query doesn't support where keyword*

    Conditions apply because you can use where condition for sub-select statements. You can perform complicated inserts using sub-selects.

    For example:

    INSERT INTO suppliers
    (supplier_id, supplier_name)
    SELECT account_no, name
    FROM customers
    WHERE city = 'Newark';
    

    By placing a "select" in the insert statement, you can perform multiples inserts quickly.

    With this type of insert, you may wish to check for the number of rows being inserted. You can determine the number of rows that will be inserted by running the following SQL statement before performing the insert.

    SELECT count(*)
    FROM customers
    WHERE city = 'Newark';
    

    You can make sure that you do not insert duplicate information by using the EXISTS condition.

    For example, if you had a table named clients with a primary key of client_id, you could use the following statement:

    INSERT INTO clients
    (client_id, client_name, client_type)
    SELECT supplier_id, supplier_name, 'advertising'
    FROM suppliers
    WHERE not exists (select * from clients
    where clients.client_id = suppliers.supplier_id);
    

    This statement inserts multiple records with a subselect.

    If you wanted to insert a single record, you could use the following statement:

    INSERT INTO clients
    (client_id, client_name, client_type)
    SELECT 10345, 'IBM', 'advertising'
    FROM dual
    WHERE not exists (select * from clients
    where clients.client_id = 10345);
    

    The use of the dual table allows you to enter your values in a select statement, even though the values are not currently stored in a table.

    See also How to insert with where clause

    0 讨论(0)
  • 2020-11-22 06:59

    You use the WHERE clause for UPDATE queries. When you INSERT, you are assuming that the row doesn't exist.

    The OP's statement would then become;

    UPDATE Users SET weight = 160, desiredWeight = 45 where id = 1;

    In MySQL, if you want to INSERT or UPDATE, you can use the REPLACE query with a WHERE clause. If the WHERE doesn't exist, it INSERTS, otherwise it UPDATES.

    EDIT

    I think that Bill Karwin's point is important enough to pull up out of the comments and make it very obvious. Thanks Bill, it has been too long since I have worked with MySQL, I remembered that I had issues with REPLACE, but I forgot what they were. I should have looked it up.

    That's not how MySQL's REPLACE works. It does a DELETE (which may be a no-op if the row does not exist), followed by an INSERT. Think of the consequences vis. triggers and foreign key dependencies. Instead, use INSERT...ON DUPLICATE KEY UPDATE.

    0 讨论(0)
  • 2020-11-22 06:59

    Does WHERE-clause can be actually used with INSERT-INTO-VALUES in any case?

    The answer is definitively no.

    Adding a WHERE clause after INSERT INTO ... VALUES ... is just invalid SQL, and will not parse.

    The error returned by MySQL is:

    mysql> INSERT INTO Users( weight, desiredWeight ) VALUES ( 160, 145 ) WHERE id = 1;
    ERROR 1064 (42000): 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 'WHERE id = 1' at line 1
    

    The most important part of the error message is

    ... syntax to use near 'WHERE id = 1' ...
    

    which shows the specific part the parser did not expect to find here: the WHERE clause.

    0 讨论(0)
提交回复
热议问题