Update field on SELECT - mysql

When I select a record from a table using SELECT how can I UPDATE one of that record's fields in the same statement? It's basically the combination of the following two statements:
SELECT value FROM items WHERE = id ='123'
UPDATE items SET found=1 WHERE id='123'
To mark that record as found.
Edit...
Would SELECT FOR UPDATE be appropriate in this instance?

Related

MySql - Add new row for every userId in the table

Below is the table i am trying to write a query for to add new rows for every user.
My question is how do i add a new row for every user? Which means for userId 2 I add AccId 4 and similarly for 7 and 8. Since there is no concept of for loop in sql, do i make use of while? If so, how to loop through the userIds since the IDs are not in equal increments?
something like this maybe:
Insert Into mytable (UserID, AccID)
Select UserID, max(accId)+1
From MyTable
Group By UserID
You can re-run it every time, you will create the next value.
Untested on a MySql server:
INSERT INTO MyTable ( ID, AccId )
SELECT MyValues.ID, MyValues.Espr1
FROM (SELECT MyTable.ID, Max([AccId]+1) AS Espr1
FROM MyTable
GROUP BY MyTable.ID) AS MyValues;
Basically we prefetch Id a AccId grouping the values of Id and grabbing the Max of AccId.
Then we add these rows to the main table. Repeating the query we will add the value 5 (AccId) and so on, always adding 1

Update SET with variable column names, variable values in variable rows PDO

I'm trying to update a certain column of certain row WHERE id is certain value. The thing is, the number/names of columns are variable, and so are their respective ids.
For example:
UPDATE table SET column1="hello" WHERE id = 5
UPDATE table SET column2="cucumber" WHERE id = 6
How can I do a single mysql query in PDO to do this?
First thing I tried is...
UPDATE table SET column1="hello", column4="bye" WHERE id IN(5, 6)
But that query will update BOTH of those columns in rows where it finds BOTH of those ids, and that's not what I'm looking for. Is it only possible to do this query by query?
Keep in mind that the argument after SET is variable, so the columns to be updated, their values and their respective ids are also variable.
A solution where you can just purely bind values would be great, but if I have to build the query string with escaped variables, then that's OK too.
Thank you.
You can do this
UPDATE table t1 JOIN table t2
ON t1.id= 5 AND t2.id= 6
SET t1.column1= 'hello',
t2.column2 = 'cucumber';
Or if you want to do this on a single column
UPDATE table
SET column2 = CASE id
WHEN 5 THEN 'hello'
WHEN 6 THEN ''
END
WHERE id IN(5, 6);

mySql after insert trigger, sum up a value by ID and update another table

I have two tables, Entries and mountPanels. What I want to do is when a record is inserted in mountPanels, sum up the panels in that table by the ID of the inserted record, and update the Entries table with that sum (that matches the same ID).
When I try the below AFTER INSERT trigger:
UPDATE Entries SET panels = (SELECT SUM(panels) FROM mountPanels WHERE Entries.EntryID = new.EntryID)
It sums up everything, but when I try
UPDATE Entries SET panels = (SELECT SUM(panels) FROM mountPanels WHERE Entries.EntryID = mountPanels.EntryID)
It sums up everything correctly by ID, but updates every row. I just want it to update the specific row with the ID that was entered last.
Your update statement doesn't have a where clause, so of course it's updating every row. Also, your statement doesn't seem to be using new correctly.
Try this:
UPDATE Entries SET
panels = (
SELECT SUM(panels)
FROM mountPanels
WHERE EntryID = new.EntryID)
WHERE EntryID = new.EntryID

PHP mysql update multiple row by single query SET='101' where id =1,2,3,7,9

I am trying to update rows in mysql but I have to use for loop for multiple update for single value mysql query is
update table set column1='100' where id =1
update table set column1='100' where id =6
update table set column1='100' where id =14
I am using for loop for running query multiple times with different id, I want to run single query for update all rows. Is that possible?
i want to do something like that
update table set column1='100' where id=1,6,14;
Use IN()
update table
set column1='100'
where id in (1,6,14)
or OR
update table
set column1='100'
where id = 1 OR id = 6 OR id = 14
Use IN() Operator
update table_name SET field_name='101'
where id IN(1,2,7,9)

MySQL update query on two tables but update only one table

I have two tables:
Table1: group_name, item_name, status
Table2: group_name, geo
I want to update table1. The default status is 0. I want to update the status of table1 to 1 using a single UPDATE statement.
I want to check for each row in table1 if the group_name exists in table2. If so, I will update status to 1.
I tried this but was not able to get the correct result.
UPDATE table1
SET table1.`STATUS`=1
WHERE table2.group_name=table1.group_name
How can I achieve my desired result?
you can use multiple update table syntax, so your query would be:
UPDATE table1,table2
SET table1.`STATUS`=1
WHERE table2.group_name=table1.group_name
You could use a multi-table update as others have shown. But you can also do it in a slightly simpler way with a single table update statement with a subselect:
UPDATE table1
SET STATUS = 1
WHERE group_name IN (SELECT group_name FROM table2)
Note also that you don't even need the precondition that status in all rows is initially set to zero. You can update all rows to their correct values in a single UPDATE statement:
UPDATE table1
SET STATUS = group_name IN (SELECT group_name FROM table2)
the SET is his command... the actual format is :
update "tablename"
set "columnname" =
"newvalue"
[,"nextcolumn" =
"newvalue2"...]
where "columnname"
OPERATOR "value"
[and|or "column"
OPERATOR "value"];
don't have a SQL database up and running, but I believe the UPDATE command is similar to the FROM command, so i think you have to do
UPDATE table1, table2 SET table1.'status' = 1
WHERE table2.group_name=table1.group_name