Forum Moderators: coopster

Message Too Old, No Replies

Using "ON DUPLICATE KEY REMOVE"

within a for loop

         

henry0

3:23 pm on Jan 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I will "self" generate a table with 300k rows/rand some_id.
$num will be set at 300000
this works fine
but I am concerned about possible duplicate
(never know) rand is cap, lower case and numbers and is 6 char long.
my problem:
I cannot figure the syntax to add by the end of my msq
ON DUPLICATE KEY REMOVE some_id ... WHERE ...what? since it is all rand based.

for ($i=0;$i<=$num; $i++)
{
$some_id=rand();
$some_id-> msq =( "
INSERT INTO aaaa
(some_id, zzz)
VALUES
('$some_id', 'zzz')
");
$result = $some_id->msq();

FYI
the zzz value is always 0 until any "some_id" is used then it becomes 1

eelixduppy

5:27 pm on Jan 15, 2008 (gmt 0)



Do you have the some_id field set for unique keys? If so, then an error should occur if you try to add one that isn't unique. So I guess there are two ways you can then go about this. You can have php detect the error and then try again with a different random id, or you can update the row that already contains the key using ON DUPLICATE KEY UPDATE [dev.mysql.com] to change it to another value. I would try both options and see what works best for you. If you think of another solution, be sure to let us know. :)

henry0

6:12 pm on Jan 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll rather use the DUPLICATE way
but I am clueless on implementing it.
The link example is understandable if you know the value of your inserted value
But mine is rand?

How will you write the Dupe query?

<edit>Typo</edit>