If I have Laravel eqoquent model (or pure mysql) And i try to update 10 records:
UPDATE users SET active = 1 WHERE is_admin IN (2,3);
How will I know which records are updated.
Is there any function in MySQL that will return the updated ids.
I need them
Thanks
No, there is no function in mysql that would return the ids of the records that got updated. Why? Becsuse you already know which records you want to update: those that correspond to the where criteria of your update query. There is only a function that tells you how many records got affected by your query.
If you want to know exactly which records got updated by your query, then you need to query the fields and ids of the records that could be updated before the update and then compare the values after the update. Where the values got changed, those are the records that got updated.
Alternatively, you can add a timestamp column to the table, which gets modified if a record is updated. After the update you can query which records got updated.
Related
Actually I don't know how to get last inserted records in two table.
I want to get last recently inserted all records in table.
I used LAST_INSERT_ID() function but it gives only one last record and i want multiple records in table.
Seems you want to get all records using LAST_INSERT_ID(), which can give only last inserted record, you can't get second last inserted record with adding any extra where clause or something like that. Either you could fetch all record directly or you can get last insert id first then fetch all (other) record based on your requirement.
I'm looking into this myself right now and the only way I can come up with is to retrieve the number of affected rows from the most recent query, and then select that many rows from the table, with the selection ordered by ID (or whatever is most appropriate to your use case) in Descending order, thus:
The insertion query:
INSERT INTO db.table (id,var1,var2)
VALUES
(NULL,'value1','value2')
(NULL,'value3','value4')
(NULL,'value5','value6')
Use mysql_affected_rows() to retrieve how many rows were inserted.
(In PHP this would be $number = $mysqli_object->affected_rows - your code implementation may differ)
then fetch the rows using that value as the LIMIT value:
SELECT * FROM db.table ORDER BY id DESC LIMIT $number
Ideally you'd wrap that up in a method or function dependant on your coding and use case.
I need to perform an update on a given table with a condition C, with a java driver.
If there is no row matching the condition C, i need to insert a new entity in the table.
If the row exists, then the update is enought.
To do so, is it possible to return from an update query the two following informations :
-Matched rows count
-Updated rows count
I believe the executeUpdate only return the number of rows updated.
The problem is that it might be zero if the update query doesn't update anything, so i have no way to know if 0 means no match (and i'll need to perform an insert) or no update.
Note : A workaround could be to insert a random field (or date), that would be updated everytime, but i'd prefer a better solution.
Thanks
The number of updated and matched rows are the same. Even if the row already has the values passed in the update, it will be counted as an updated row:
id name
1 foo
update mytable set name = 'foo' where id = 1;
--> 1 row updated
I have one mysql table on which i created on-after and on-before trigger for insertion. Each tigger update 2 rows respectively. So once i insert a row to the table, altogether 5 rows are updated, even though the response from the DB will be as "1 row is affected". I need to find a way to know the total no of rows got updated, in this case 5.
The problem seems to be that MySQL does not count along when you insert/update rows in a trigger. The best possible solution might be to count the manually inserted/updated rows for yourself, store the value in a variable, get it after the outer query and add the result of ROW_COUNT() to it.
I think it is giving one row affected because you are adding one row in table. Mysql reply gives information of row added in that table only. And not giving affected rows by triggers.
i am currently using the following query syntax for updating records like the following:
update items set sort_index=1 where id=11
update items set sort_index=2 where id=33
update items set sort_index=3 where id=52
so i was wondering, is it possible to update several records by ONE single query?
thanks
Yes it is possible to update multiple records with one query, it however is not possible to update multiple records with different data, all records are updated with the same "SET" criteria.
So the method your currently using is the correct method for updating those records, if you have multiple records that get their sort index updated to 1,2,3 you can collect those ids then run the following to update all collected records
UPDATE items set sort_index=1 WHERE id IN (1,2,3); //where 1, 2 and 3 are the ids
Yes, you can update row(s) based upon the give where expression.
I don't think this is possible as I couldn't find anything but I thought I would check on here in case I am not searching for the correct thing.
I have a settings table in my database which has two columns. The first column is the setting name and the second column is the value.
I need to update all of these at the same time. I wanted to see if there was a way to update these values at the same time one query like the following
UPDATE table SET col1='setting name' WHERE col2='1 value' AND SET col1='another name' WHERE col2='another value';
I know the above isn't a correct SQL format but this is the sort of thing that I would like to do so was wondering if there was another way that this can be done instead of having to perform separate SQL queries for each setting I want to update.
Thanks for your help.
You can use INSERT INTO .. ON DUPLICATE KEY UPDATE to update multiple rows with different values.
You do need a unique index (like a primary key) to make the "duplicate key"-part work
Example:
INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
ON DUPLICATE KEY UPDATE b = VALUES(b), c = VALUES(c);
-- VALUES(x) points back to the value you gave for field x
-- so for b it is 2 and 5, for c it is 3 and 6 for rows 1 and 4 respectively (if you assume that a is your unique key field)
If you have a specific case I can give you the exact query.
UPDATE table
SET col2 =
CASE col1
WHEN 'setting1'
THEN 'value'
ELSE col2
END
, SET col1 = ...
...
I decided to use multiple queries all in one go. so the code would go like
UPDATE table SET col2='value1' WHERE col1='setting1';
UPDATE table SET col2='value2' WHERE col1='setting1';
etc
etc
I've just done a test where I insert 1500 records into the database. Do it without starting a DB transaction and it took 35 seconds, blanked the database and did it again but starting a transaction first, then once the 1500th record inserted finish the transaction and the time it took was 1 second, so definetely seems like doing it in a db transaction is the way to go.
You need to run separate SQL queries and make use of Transactions if you want to run as atomic.
UPDATE table SET col1=if(col2='1 value','setting name','another name') WHERE col2='1 value' OR col2='another value'
#Frits Van Campen,
The insert into .. on duplicate works for me.
I am doing this for years when I want to update more than thousand records from an excel import.
Only problem with this trick is, when there is no record to update, instead of ignoring, this method inserts a record and on some instances it is a problem. Then I need to insert another field, then after import I have to delete all the records that has been inserted instead of update.