I have two tables one table has an id
and a username
the name of the table is user
. I have another table called value
that table has an id
which is to store the id from the user
table. The table value
also has a column called value
and item_id
which is used to store the item.
I want to write an update statement that updates the following columns within value id, value, item_id
however the values I have to execute the statement is username = $username, value = $value and item_id=$item_id
(based on the application)
How can I write an update statement that stores the id
(username id), value
and item_id
The reason for using an update statement is because that user can change the value within the value
column at any time
UPDATE `value` SET `value` = $value WHERE item_id = $item_id
AND user_id IN (SELECT id FROM `users` WHERE username = $username)
be sure to sanitize your database inputs.
this will only update, if you want to create the entry (i assume that user_id and item_id are the combined primary key of your second table), you can use a replace into statement.
please tell me if you need that, because i think it was not the question.
it may be not as efficient but i think that will not really matter, but you can of course fetch the user id first with a separate query.
this will probably be better understandable.
Try this
UPDATE value, user
SET value = $value
WHERE item_id = $item_id
AND value.user_id = user.user_id
AND user.username = $username;
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.