To update multiple columns, you use a list comma-separated assignments. You supply the value in each column’s assignment in the form of a literal value, an expression, or a subquery. Third, specify which rows to be updated using a condition in the WHERE clause. Updating multiple columns and multiple rows with one MySQL query. Since you asked this question in the mysql forum, let me give you a mysql answer. I guess you must reconcile yourself to. Oh, absolutely!! Your problem likely lies in the php code, something about constructing variables and looping (i'm guessing -- i don't do php so i only gave your code a cursory glance).
I need to update 2 datetime columns, and I need them to be exactly the same, using mysql version 4.1.20. I'm using this query:
It is safe or there is a chance that the columns are update with different time, because of the 2 visible calls to now()
?
I don't think that it can be update with different values (I think internally mysql calls now()
just once per row or something similar), but I'm not an expert, what do you think?
Update:Second question was extracted here.
7 Answers
Found a solution:
I found this in MySQL Docs and after a few tests it works:
the following statement sets col2 to the current (updated) col1 value, not the original col1 value. The result is that col1 and col2 have the same value. This behavior differs from standard SQL.
UPDATE t1 SET col1 = col1 + 1, col2 = col1;
Radu MarisRadu Marishttps://modeomg.netlify.app/epson-tm-u295-driver-xp.html. Mysql isn't very clever. When you want to use the same timestamp in multiple update or insert queries, you need to declare a variable.
When you use the now()
function, the system will call the current timestamp every time you call it in another query.
You can store the value of a now() in a variable before running the update query and then use that variable to update both the fields last_update
and last_monitor
.
This will ensure the now() is executed only once and same value is updated on both columns you need.
Sachin ShanbhagMysql Update Table Field
Sachin ShanbhagYou can put the following code on the default value of the timestamp column:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
, so on update the two columns take the same value.
MySQL evaluates now() once per statement when the statement commences execution. So it is safe to have multiple visible now() calls per statement.
If you really need to be sure that now()
has the same value you can run two queries (that will answer to your second question too, in that case you are asking to update last_monitor = to last_update
but last_update
hasn't been updated yet)
HP Deskjet 1510 All-in-One Printer series Full Feature Software and Drivers Free Get the complete set of drivers, installer, and software for your HP Deskjet 1510 series printer. Driver impressora hp deskjet 840c windows 7 64 bits. Home » HP DeskJet 840C/841C/842C/843C Use the links on this page to download the latest version of HP DeskJet 840C/841C/842C/843C drivers. All drivers available for download have been scanned by antivirus program.
you could do something like:
anyway I think that mysql is clever enough to ask for now()
only once per query.
Sql Update Multiple Columns
blackpantherThere are 2 ways to this;
First, I would advice you declare now() as a variable before injecting it into the sql statement. Lets say;
Logically if you want a different input for last_monitor then you will add another variable like;
Mysql Set Multiple Values
This way you can use the variables as many times as you can, not only in mysql statements but also in the server-side scripting-language(like PHP) you are using in your project.Remember these same variables can be inserted as inputs in a form on the front-end of the application. That makes the project dynamic and not static.
Secondly if now() indicates time of update then using mysql you can decalre the property of the row as a timestamp. Every time a row is inserted or updated time is updated too.
Not the answer you're looking for? Browse other questions tagged mysqlsql-update or ask your own question.
I have a table of postcodes and I want to update each postcode with its 3 nearest neighbours. Ie to fill in the blanks in this table:
I've figured out a SELECT query to find the nearest postcodes and here is one clumsy way the first row could be updated:
However this will result in 3 select queries being run for each row update. It would be more efficient if there was some way to do what is expressed by this pseudo code:
The 'select query' in the above looks like this:
Is there anyway for the rows returned from the select to be put into a form that they can be used to update multiple fields?Thanks.
4 Answers
ThomasThomasYou can do something similar to this:
I found this related question on Stackoverflow on how to transform columns to rows:
In your case, you can do something like
Here is a hack to simulate the ROW_NUMBER() functionality in MySQL [1]:
I think you could do this with the pseudo-code:
it'd be easier to specify it seeing the real SQL.
Note the first column is specified as a constant in quotes. For this to work postcode
must be a UNIQUE
or PRIMARY
index.
Anytime I see a table with columns that have 1-up counters after their names, I get concerned.
In general, it is a Bad Idea (TM) to store data that can be calculated from data that is already stored. What happens if your application all of a sudden needs the 4 closest postal codes? What if the postal code boundaries change?
Assuming the distance calculation isn't very complex, you'll be better off in the long run not explicitly storing this data.