Jump to content

Marking every 5 rows


Canman2005

Recommended Posts

Hi all

 

Wondering if you can help;

 

I have a simple table called `mydata`, it has the following fields

 

`id`

`checked`

`date`

 

I have the following INSERT script

 

INSERT INTO `mydata` SET `checked`='1', `date`=date('Y-m-d')

 

What I want to do is for every 5 rows of data INSERTED into my table, 1 of those 5 rows INSERTED to have the field `checked` set to 1 and the other 4 rows set to 0.

 

A history of INSERTS might look like

 

INSERT INTO `mydata` SET `checked`='0', `date`=date('Y-m-d')
INSERT INTO `mydata` SET `checked`='1', `date`=date('Y-m-d')
INSERT INTO `mydata` SET `checked`='0', `date`=date('Y-m-d')
INSERT INTO `mydata` SET `checked`='0', `date`=date('Y-m-d')
INSERT INTO `mydata` SET `checked`='0', `date`=date('Y-m-d')

 

INSERT INTO `mydata` SET `checked`='0', `date`=date('Y-m-d')
INSERT INTO `mydata` SET `checked`='0', `date`=date('Y-m-d')
INSERT INTO `mydata` SET `checked`='0', `date`=date('Y-m-d')
INSERT INTO `mydata` SET `checked`='1', `date`=date('Y-m-d')
INSERT INTO `mydata` SET `checked`='0', `date`=date('Y-m-d')

 

INSERT INTO `mydata` SET `checked`='1', `date`=date('Y-m-d')
INSERT INTO `mydata` SET `checked`='0', `date`=date('Y-m-d')
INSERT INTO `mydata` SET `checked`='0', `date`=date('Y-m-d')
INSERT INTO `mydata` SET `checked`='0', `date`=date('Y-m-d')
INSERT INTO `mydata` SET `checked`='0', `date`=date('Y-m-d')

 

As you can see, the INSERTS are split into 5's, because I want 1 out of every 5 rows INSERTED to have its value of the field `checked` to be set to 1, the other 4 rows should have the value set to 0.

 

This should be a random thing, so it might set the `checked` value of the first row INSERTED to 1 or it might set the last row INSERTED to 1.

 

Does that make much sense?

 

Thanks

 

Dave

Link to comment
Share on other sites

That works cool, but it always does

 

SET checked='1'

 

on the 5th row inserted.

 

I'm trying to get it to add it randomly to one of each block of 5 rows that are inserted.

 

So it might do

 

SET checked='1'

 

on the 2nd row inserted or it might do it on the 4th row

 

but im trying to keep to blocks of 5

Link to comment
Share on other sites

So it could look like

 

SET checked='0'

SET checked='1'

SET checked='0'

SET checked='0'

SET checked='0'

 

SET checked='0'

SET checked='0'

SET checked='0'

SET checked='0'

SET checked='1'

 

SET checked='1'

SET checked='0'

SET checked='0'

SET checked='0'

SET checked='0'

 

Link to comment
Share on other sites

<?php
$array[0][0] = 1;
$array[0][1] = 2;
$array[0][2] = 3;
$array[0][3] = 4;
$array[0][4] = 5;

$array[1][0] = 6;
$array[1][1] = 7;
$array[1][2] = 8;
$array[1][3] = 9;
$array[1][4] = 10;

for( $i = 0; $i < count( $array ); $i++ )
{
	$randomNum = rand(0, 4);
	echo "Row ID:  " . $array[$i][$randomNum] . " will have checked='1'<br />";
                // Run SQL update.
}
?>

 

This will let you grab a random ID, but you would need to store the inserted ID's in an array.

Link to comment
Share on other sites

Hi

 

Keeping it simple and putting all the inserts into one statement.

 

$RandNum = rand(1,5);
$sql = "INSERT INTO `mydata` (`checked`, `date`)
VALUES (".(($RandNum = 1) ? '1' : '0' ).",date('Y-m-d')),
VALUES (".(($RandNum = 2) ? '1' : '0' ).",date('Y-m-d')),
VALUES (".(($RandNum = 3) ? '1' : '0' ).",date('Y-m-d')),
VALUES (".(($RandNum = 4) ? '1' : '0' ).",date('Y-m-d')),
VALUES (".(($RandNum = 5) ? '1' : '0' ).",date('Y-m-d'))";

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.