MySQL Insert query doesn't work with WHERE clause
MySQL INSERT Syntax does not support the WHERE clause so your query as it stands will fail. Assuming your id
column is unique or primary key:
If you're trying to insert a new row with ID 1 you should be using:
INSERT INTO Users(id, weight, desiredWeight) VALUES(1, 160, 145);
If you're trying to change the weight/desiredWeight values for an existing row with ID 1 you should be using:
UPDATE Users SET weight = 160, desiredWeight = 145 WHERE id = 1;
If you want you can also use INSERT .. ON DUPLICATE KEY syntax like so:
INSERT INTO Users (id, weight, desiredWeight) VALUES(1, 160, 145) ON DUPLICATE KEY UPDATE weight=160, desiredWeight=145
OR even like so:
INSERT INTO Users SET id=1, weight=160, desiredWeight=145 ON DUPLICATE KEY UPDATE weight=160, desiredWeight=145
It's also important to note that if your id
column is an autoincrement column then you might as well omit it from your INSERT all together and let mysql increment it as normal.
MySQL direct INSERT INTO with WHERE clause
INSERT
syntax cannot have WHERE
clause. The only time you will find INSERT
has WHERE
clause is when you are using INSERT INTO...SELECT
statement.
The first syntax is already correct.
Insert query with WHERE condition in mysql
IMPORTANT WARNING: mysql_query was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include mysqli_query() and PDO::query()
FOR EDUCATIONAL PURPOSES: this is how your code should look if you are using mysql_query:
//you should definitely create a function that sanitizes the users input
//so that you don't get hacked via sql injection:
$value = sanitize($value);
$id= sanitize($id);
$sql = "UPDATE employee SET annual_additional = '$value'
WHERE emp_id='$id'";
if (!result = mysql_query($sql))
{
die("query error".mysql_error());
}
How to add a where clause in a MySQL Insert statement?
In an insert statement you wouldn't have an existing row to do a where claues on? You are inserting a new row, did you mean to do an update statment?
update users set username='JACK' and password='123' WHERE id='1';
Insert into query with where clause
There's no INSERT
with where clause.
Take a look at this question: How to insert with where clause
Maybe, you can create an IF
inside your app to verify your parameters before executing the INSERT
statement.
Based on your query, I believe you need an UPDATE
:
UPDATE waardes SET tag_name = @tagName,
int_value = @intValue, real_value = @doubleValue,
bool_value = @boolValue WHERE machine.machine_id LIKE %1%
Is this what you need?
Related Topics
How to Delete Duplicate Records in MySQL Database
Error Related to Only_Full_Group_By When Executing a Query in MySQL
How to Select the First Row of Each Group
Solutions For Insert or Update on SQL Server
How to Concatenate Strings of a String Field in a Postgresql 'Group By' Query
How to Declare a Variable in MySQL
How to Delete from Multiple Tables in MySQL
SQL Server: How to Insert into Two Tables At the Same Time
How to Combine Date from One Field With Time from Another Field - Ms SQL Server
MySQL Results as Comma Separated List
How to Create a Table from Select Query Result in SQL Server 2008
What's the Difference Between Truncate and Delete in Sql
Ora-12560: Tns:Protocol Adaptor Error
How to Request a Random Row in Sql
What Is the Benefit of Using "Set Xact_Abort On" in a Stored Procedure