oliverbj
oliverbj

Reputation: 6050

Set row as another row - MySQL

I have this query:

 mysql_query("
    UPDATE users SET

    `clicks_yesterday`=`clicks_today`,`clicks_today`=0) 

My structure in my database looks like this: Database Structure

My question is, how can I do so whenever I run the query above, clicks_yesterday get's the value of clicks_today?

Regards

Upvotes: 0

Views: 54

Answers (2)

Andreas Wederbrand
Andreas Wederbrand

Reputation: 40001

For MySQL what you have written works. It is however different on different DBs so it's important to test.

These are my tests for this question

CREATE TABLE `duals` (
  `one` int(11) DEFAULT NULL,
  `two` int(11) DEFAULT NULL
);

insert into duals values (1, 2);

select * from duals;
+------+------+
| one  | two  |
+------+------+
|    1 |    2 |
+------+------+

update duals set one = two, two = 0;

select * from duals;
+------+------+
| one  | two  |
+------+------+
|    2 |    0 |
+------+------+

Upvotes: 0

Marc B
Marc B

Reputation: 360732

That's how you do it. assignments in SQL are evaluated in the order encountered (e.g. left -> right). But if you want to be ENTIRELY sure that things are assigned properly, then split it into two queries:

UPDATE users SET clicks_yesterday = clicks_today;
UPDATE users SET clicks_today = 0;

Upvotes: 1

Related Questions