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.
Is it possible in sql to delete since one row until the end of the table ?
for instance :
delete from mytable where oneDate ='2017-06-06';
and since this row delete all following rows?
Yes it is.
Example:
DELETE FROM [table] WHERE [table.ID]>=50
Assuming the table has 100 records it would delete from 50 to 100, keeping the first 50. Same with dates and having into consideration some criteria to filter from some specific point on (in my example, the ID).
I would like to query a database table for some of it's oldest entries and update them with a second query afterwards.
But how can I prevent that another process (that does the same) will return the same rows by the SELECT query and the UPDATE part will modify the entries twice?
As far as I see a simple transaction cannot prevent this from happening.
Use the SELECT ... FOR UPDATE mechanism to do this (see http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html)
Would this SQL query work for an SQL database?
UPDATE tableName SET colName1='value' WHERE colName2='Id_1' OR colName2='Id_2'
Basically, can I update the same column for multiple entries by checking against another column twice?
For example(as shown above), set colName1 to a value for 2 seperate entries which are identified by two different IDs
Yes that would work but you can make it more efficient like this:
UPDATE tableName SET colName1='value' WHERE colName2 in('Id_1','Id_2')
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.