| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

07-10-07, 18:51
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 15
|
|
|
datestamp the purchase_date field for all rows
|
|
I know it is simply enough to get a bunch of rows from a table and loop through them in PHP to change thesame field in all rows, eg. set them to NOW(), but can this be done purely in MySQL with a query. Can subqueries perform changes to the table the main query is operating on yet?
For instance, I want to datestamp the purchase_date field for all rows matching my WHERE clause with todays datetime. Would be neat to have a single query to do this.
Thanks,
Nick
|
|

07-10-07, 18:54
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 15
|
|
|
Column update?
I know it is simply enough to get a bunch of rows from a table and loop through them in PHP to change the same field in all rows, eg. set them to NOW(), but can this be done purely in MySQL with a query. Can subqueries perform changes to the table the main query is operating on yet?
For instance, I want to datestamp the purchase_date field for all rows matching my WHERE clause with todays datetime. I guess it could be called a column update. Would be neat to have a single query to do this.
Thanks,
Nick
|
|

07-10-07, 18:56
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 15
|
|
|
|
Apologies, I meant to put that in a new thread.
|
|

07-10-07, 19:28
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
|
|
no apologies necessary, the friendly moderators are here to help you
can this be done purely in MySQL with a query?
yes

|
|

07-10-07, 19:30
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
|
|
see? we can even merge threads 
|
|

07-10-07, 19:41
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 15
|
|
Could you please show me how?
Thanks,
Nick.
|
|

07-10-07, 19:55
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
|
|
Quote:
|
Originally Posted by nickweavers
I want to datestamp the purchase_date field for all rows matching my WHERE clause with todays datetime.
|
i will have to guess at a lot of this, because you did not give too many details...
Code:
update daTable
set purchase_date = current_date
where somecolumn = 'foo'
and someothercolumn = 937
|
|

07-11-07, 03:35
|
|
Registered User
|
|
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
|
|
A possible solution? :
1) Add a timestamp column to your table
Code:
ALTER TABLE <tablename>
ADD COLUMN `update_datetime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
2) As in Rudy's query (except with a minor change) :
Code:
UPDATE <tablename>
SET update_datetime = NULL
WHERE somecolumn = 'foo'
AND someothercolumn = 937
The advantage of using a TIMESTAMP column type is that if you choose to update another column (say `name`) the update_datetime column will automagically update itself with the latest datetime....
|
|

07-11-07, 05:47
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
|
|
ah, but the problem with using a timestamp—or a datetime, for that matter—is that when you then go to write a query to pull all the inventory items purchased yesterday, you cannot write the query using
... where purchase_date = date_sub(current_date,interval 1 day)

|
|

07-11-07, 08:48
|
|
Registered User
|
|
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
|
|
Surely you could just change the "=" to a ">"
Or if you want to rule out todays updated also then
Code:
... WHERE purchase_date
BETWEEN date_sub(current_date,interval 1 day)
AND CURRENT_DATE;
|
Last edited by aschk; 07-11-07 at 08:52.
|

07-11-07, 08:50
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
|
|
yes, you could, and please don't call me shirley
but i do hope you see my point -- why assign a timestamp or datetime value to something that is essentially a date
|
|

07-11-07, 09:01
|
|
Registered User
|
|
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
|
|
Yup i shirley  do see your point. I suppose it depends on how granular the updated information needs to be.
Also came across another useful feature (ONLY AVAILABLE SINCE version 5.0.0)
Code:
WHERE 1 =
TIMESTAMPDIFF(DAY,CURRENT_DATE,purchase_date);
Checks the number of days between a timestamp and datetime. Pretty cool.
|
|

07-11-07, 09:21
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
|
|
pretty cool but really really slow

|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|