Page 1 of 1
Display x number of records with a specified id in the middl
Posted: Wed Dec 17, 2008 2:41 am
by LonelyProgrammer
Hi,
I am trying to come up with a SQL statement that will display x (say 10) records, with the one meeting the search criterion in the center as the x/2 record (or the 5th record). I could try this
Code: Select all
SELECT * FROM RECORDS WHERE id <= wanted-x/2 AND id > wanted+x/2
...but it may not give me 10 records if some were removed. Any suggestions?
Re: Display x number of records with a specified id in the middl
Posted: Wed Dec 17, 2008 3:16 am
by jaoudestudios
Not sure what you are trying to do, but to limit your results to 10 records use the LIMIT command.
i.e.
Re: Display x number of records with a specified id in the middl
Posted: Wed Dec 17, 2008 7:47 am
by LonelyProgrammer
Example:I specify a userid of 100, so table will display the records of users with userid from 95 to 105.
Re: Display x number of records with a specified id in the middl
Posted: Wed Dec 17, 2008 8:36 am
by jaoudestudios
Example:I specify a userid of 100, so table will display the records of users with userid from 95 to 105.
So if you have 5 records deleted (i.e. 97,98,99,100,101), you will only be returned 5 results (i.e. 95,96,102,103,104), but you want it to always return 10? i.e continue until a limit of 10 is reached, in this case...105,106,107,108,109?
Re: Display x number of records with a specified id in the middl
Posted: Wed Dec 17, 2008 9:24 am
by VladSun
Code: Select all
$query = "
(select * from records where id < ".$id." limit ".$delta.")
union
(select * from users where id >= ".$id." limit ".($delta + 1).")
";
Re: Display x number of records with a specified id in the middl
Posted: Fri Dec 19, 2008 1:59 am
by LonelyProgrammer
This works beautifully. Thanks!