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.

 
Go Back  dBforums > Database Server Software > MySQL > datestamp the purchase_date field for all rows

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-10-07, 18:51
nickweavers nickweavers is offline
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
Reply With Quote
  #2 (permalink)  
Old 07-10-07, 18:54
nickweavers nickweavers is offline
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
Reply With Quote
  #3 (permalink)  
Old 07-10-07, 18:56
nickweavers nickweavers is offline
Registered User
 
Join Date: Jan 2005
Posts: 15
Apologies, I meant to put that in a new thread.
Reply With Quote
  #4 (permalink)  
Old 07-10-07, 19:28
r937 r937 is offline
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

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 07-10-07, 19:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
see? we can even merge threads
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 07-10-07, 19:41
nickweavers nickweavers is offline
Registered User
 
Join Date: Jan 2005
Posts: 15
Could you please show me how?

Thanks,
Nick.
Reply With Quote
  #7 (permalink)  
Old 07-10-07, 19:55
r937 r937 is offline
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 07-11-07, 03:35
aschk aschk is offline
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....
Reply With Quote
  #9 (permalink)  
Old 07-11-07, 05:47
r937 r937 is offline
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)

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 07-11-07, 08:48
aschk aschk is offline
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.
Reply With Quote
  #11 (permalink)  
Old 07-11-07, 08:50
r937 r937 is offline
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 07-11-07, 09:01
aschk aschk is offline
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.
Reply With Quote
  #13 (permalink)  
Old 07-11-07, 09:21
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
pretty cool but really really slow

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On