How do I insert into database with an SQL WHERE clause? (Diesel)

How do I use Diesel to do this SQL:

INSERT INTO Customer (FirstName, LastName) FROM Supplier WHERE ID = 1001

I'm stuck with how to do the "WHERE" clause using Diesel. The documentation (Insert from Select section) seems to wordy doesn't seem to be what I need to there could be a simpler way.

NOTE: The operation is like a replacement (update data) on an already existing record.

I think it is "update".

The sql you have posted is not valid as you can't have a where clause on an insert. To update a specific row, you should use an sql update query.

UPDATE Customer SET FirstName = 'foo', LastName = 'bar' WHERE ID = 1001;

or to update it with an insert, you need something like this:

INSERT INTO Customer(FirstName, LastName) VALUES ('foo', 'bar')
ON DUPLICATE KEY UPDATE FirstName = 'foo', LastName = 'bar';

or to insert from another table

INSERT INTO Customer(FirstName, LastName)
SELECT (FirstName, LastName) FROM Supplier
WHERE ID = 1001;

If you have trouble with converting any of these to diesel, please first tell us which one you want.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.