[nycphp-talk] every other record

Adam Maccabee Trachtenberg adam at
Wed Feb 1 17:36:01 EST 2006

On Wed, 1 Feb 2006, Rahmin Pavlovic wrote:

> To the more knowledgeable SQL heads in the house:
> Do you think it's reliable to mod() the record id to pull every other
> record, like:
> select * from table where mod(id,2)=0 #or
> select * from table where mod(id,2)=1
> Or do you think it would be more reliable to manually pull 'em, like:
> select * from table limit 1,1
> select * from table limit 3,1
> etc.
> MySQL 5.0.2
> mod() appears to work, but it also seems to behave a lil wonky.

I see many people have provided a number good of answers, so let me
step back and ask: "What problem are you trying to solve?"

Why do you need every other row? Is this data likely to change? How
often is it updated? Can you pre-compute an ENUM that you insert upon
writing the table that lets you do:

SELECT * FROM TABLE WHERE filter = 'even';
SELECT * FROM TABLE WHERE filter = 'odd';

That avoids the computation on each call and avoids the issue with
holes in your id sequences.

Or can you shove things in different tables and when you need both
join them up?


adam at |
author of o'reilly's "upgrading to php 5" and "php cookbook"
avoid the holiday rush, buy your copies today!

More information about the talk mailing list