简体   繁体   中英

How to Insert new value into table using where

I have one table name "Grade" with this structure:

ID  Student_ID First_Name Last_Name Grade
1       1         John      Smith    60
2       2         Garry     Poul     70
3       1         John      Smith    80

And I want to add a new grade for Student_ID = 1 in the table Grade , I am using PHP with MySQL DB.

I used this but gives me error!

$sql = "INSERT INTO Grade (Grade) VALUES ('85') WHERE Student_ID=1 ";

During search I found that I can't use WHERE with INSERT in MySQL, how can solve it?

Thanks for all

For updating existing records use UPDATE and not INSERT

UPDATE Grade 
SET Grade = 85  
WHERE Student_ID = 1 

Insert: If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query

$sql = "INSERT INTO Grade (Grade) VALUES ('85') WHERE Student_ID=1 ";

Update : when updating records in a table. Notice the WHERE clause in the UPDATE statement. The WHERE clause specifies which record(s) that should be updated, If you omit the WHERE clause, all records in the table will be updated!

UPDATE Grade SET Grade = 85 WHERE Student_ID=1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM