You are here: Re: SELECT * help needed. « PHP Programming Language « IT news, forums, messages
Re: SELECT * help needed.

Posted by Tim Roberts on 10/21/05 08:50

"Simon" <spambucket@example.com> wrote:
>Hi,
>
>If I have a lot of articles, all with a unique IDs.
>
>First I would like to search for that ID, I could do
>
>SELECT * from ARTICLE where ID = xx
>
>But I also want to display the 5 articles before and after that article.
>
>SELECT * from ARTICLE where ID > xx LIMIT 0, 10
>and
>SELECT * from ARTICLE where ID < xx LIMIT 0, 10

This is incorrect. SQL tables have no inherent ordering. Thus, the first
query will get 10 records with ID numbers larger than xx, but there is
absolutely no guarantee that they will be the records immediately above xx,
nor that they will be ordered in any way.

Given the limitations you described, there is no reliable way to do this
other than:

SELECT * FROM article WHERE ID < xx ORDER BY id DESC LIMIT 10
UNION
SELECT * FROM article WHERE ID > xx ORDER BY id LIMIT 10;
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация