473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

UPDATE step thru every record w/out unique id field

Rey
How can I update every record of a given table one by one.
I need to update a field (date) with a different, random date for each
record.

loop {

generate random date;
update one record with new date;
goto new record

}

Problem. Table does not have a unique id field

table (item, date)

peaches - 2003-05-13
pears - 2003-11-24
peaches - 2004-01-04

the table is a given... meaning I have to work with it the way it is.

Thanks,
Rey
Jul 19 '05 #1
6 3803
Rey wrote:
How can I update every record of a given table one by one.
I need to update a field (date) with a different, random date for each
record.


update tablename set date=(*);

Where you need to replace (*) with a sql code that creates a random date
for you. See function called rand() to get started with it. You can
either call rand once to create the date in days and convert the value
into date. Or you can call int 3 times, 1 for year, 1 for month and 1
for day, and create the date from those values. Let us know if this gets
too difficult for you.

For rand():
http://www.mysql.com/doc/en/Mathematical_functions.html

For date and time functions which you need when converting rand() values
into dates:
http://www.mysql.com/doc/en/Date_and...functions.html
Jul 19 '05 #2
Rey wrote:
How can I update every record of a given table one by one.
I need to update a field (date) with a different, random date for each
record.


update tablename set date=(*);

Where you need to replace (*) with a sql code that creates a random date
for you. See function called rand() to get started with it. You can
either call rand once to create the date in days and convert the value
into date. Or you can call int 3 times, 1 for year, 1 for month and 1
for day, and create the date from those values. Let us know if this gets
too difficult for you.

For rand():
http://www.mysql.com/doc/en/Mathematical_functions.html

For date and time functions which you need when converting rand() values
into dates:
http://www.mysql.com/doc/en/Date_and...functions.html
Jul 19 '05 #3
Rey
Will this not set the SAME date for all records?

Rey
"Aggro" <sp**********@yahoo.com> wrote in message
news:vT**************@read3.inet.fi...
Rey wrote:
How can I update every record of a given table one by one.
I need to update a field (date) with a different, random date for each
record.


update tablename set date=(*);

Where you need to replace (*) with a sql code that creates a random date
for you. See function called rand() to get started with it. You can
either call rand once to create the date in days and convert the value
into date. Or you can call int 3 times, 1 for year, 1 for month and 1
for day, and create the date from those values. Let us know if this gets
too difficult for you.

For rand():
http://www.mysql.com/doc/en/Mathematical_functions.html

For date and time functions which you need when converting rand() values
into dates:
http://www.mysql.com/doc/en/Date_and...functions.html

Jul 19 '05 #4
Rey
Will this not set the SAME date for all records?

Rey
"Aggro" <sp**********@yahoo.com> wrote in message
news:vT**************@read3.inet.fi...
Rey wrote:
How can I update every record of a given table one by one.
I need to update a field (date) with a different, random date for each
record.


update tablename set date=(*);

Where you need to replace (*) with a sql code that creates a random date
for you. See function called rand() to get started with it. You can
either call rand once to create the date in days and convert the value
into date. Or you can call int 3 times, 1 for year, 1 for month and 1
for day, and create the date from those values. Let us know if this gets
too difficult for you.

For rand():
http://www.mysql.com/doc/en/Mathematical_functions.html

For date and time functions which you need when converting rand() values
into dates:
http://www.mysql.com/doc/en/Date_and...functions.html

Jul 19 '05 #5
Rey wrote:
Will this not set the SAME date for all records?


Small example:

mysql> select * from randomdate;
+------------+
| randomdate |
+------------+
| 2004-04-04 |
| 2004-04-04 |
| 2004-04-04 |
| 2004-04-04 |
| 2004-04-04 |
| 2004-04-04 |
+------------+

mysql> update randomdate set randomdate=
date_sub(curdate(),interval rand()*365 day);

mysql> select * from randomdate;
+------------+
| randomdate |
+------------+
| 2003-11-05 |
| 2003-09-10 |
| 2003-08-31 |
| 2003-12-24 |
| 2003-08-25 |
| 2004-01-15 |
+------------+
Jul 19 '05 #6
Rey wrote:
Will this not set the SAME date for all records?


Small example:

mysql> select * from randomdate;
+------------+
| randomdate |
+------------+
| 2004-04-04 |
| 2004-04-04 |
| 2004-04-04 |
| 2004-04-04 |
| 2004-04-04 |
| 2004-04-04 |
+------------+

mysql> update randomdate set randomdate=
date_sub(curdate(),interval rand()*365 day);

mysql> select * from randomdate;
+------------+
| randomdate |
+------------+
| 2003-11-05 |
| 2003-09-10 |
| 2003-08-31 |
| 2003-12-24 |
| 2003-08-25 |
| 2004-01-15 |
+------------+
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rey | last post by:
How can I update every record of a given table one by one. I need to update a field (date) with a different, random date for each record. loop { generate random date; update one record with...
8
by: Jan van Veldhuizen | last post by:
The UPDATE table FROM syntax is not supported by Oracle. I am looking for a syntax that is understood by both Oracle and SqlServer. Example: Table1: id name city ...
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
11
by: Siv | last post by:
Hi, I seem to be having a problem with a DataAdapter against an Access database. My app deletes 3 records runs a da.update(dt) where dt is a data.Datatable. I then proceed to update a list to...
5
by: Louis LeBlanc | last post by:
Hey folks. I'm new to the list, and not quite what you'd call a DB Guru, so please be patient with me. I'm afraid the lead up here is a bit verbose . . . I am working on an application that...
7
by: lmnorms1 | last post by:
Hello, I am trying to update an access database record date field that matches a specific date. The code is not working. Anyone have any advice? Here is the code: Dim gConnString As String =...
2
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
22
by: pbd22 | last post by:
hi. I am having probelms with an update statement. every time i run it, "every" row updates, not just the one(s) intended. so, here is what i have. i have tried this with both AND and OR and...
1
by: jmarcrum | last post by:
Hey everyone, I have a question that's been troubling me for a bit. I work for Alabama Power. I have 1 table (tblOutages). Every morning I import a list of Power Outages for the state of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.