Connecting Tech Pros Worldwide Forums | Help | Site Map

selecting only a certain number of bits of data

roondog
Guest
 
Posts: n/a
#1: Apr 16 '07
I have just started learning php and mysql. I am trying to make a news page
for a site where you can enter news in a form and it appears on the news
page. That is done, fairly simple. I've also ordered them with the latest
first. I would now like to only show a certain number of the news articles.
Say the latest five. Each article is numbered so I guess I would use that
but i'm not really sure how to do it. Any ideas?




Tyno Gendo
Guest
 
Posts: n/a
#2: Apr 16 '07

re: selecting only a certain number of bits of data


roondog wrote:
Quote:
I have just started learning php and mysql. I am trying to make a news page
for a site where you can enter news in a form and it appears on the news
page. That is done, fairly simple. I've also ordered them with the latest
first. I would now like to only show a certain number of the news articles.
Say the latest five. Each article is numbered so I guess I would use that
but i'm not really sure how to do it. Any ideas?
>
>
>
If you have a date field you can order by date descending, but ID is
fine if you happy with that... so...

SELECT * FROM news ORDER BY news_id DESC LIMIT 5
Toby A Inkster
Guest
 
Posts: n/a
#3: Apr 16 '07

re: selecting only a certain number of bits of data


roondog wrote:
Quote:
I would now like to only show a certain number of the news articles.
Say the latest five.
Certain databases allow you to use a "LIMIT" clause to limit the number of
results returned by a SELECT query. Because this is not part of the
official SQL standard, the exact syntax varies between databases. Here are
some examples:

MySQL:
SELECT * FROM articles ORDER BY created_date DESC LIMIT 5;

PostgreSQL:
SELECT * FROM articles ORDER BY created_date DESC LIMIT 5;

Microsoft SQL Server:
SELECT TOP 5 * FROM articles ORDER BY created_date DESC;

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
roondog
Guest
 
Posts: n/a
#4: Apr 16 '07

re: selecting only a certain number of bits of data



"Toby A Inkster" <usenet200703@tobyinkster.co.ukwrote in message
news:i7vbf4-4kp.ln1@ophelia.g5n.co.uk...
Quote:
roondog wrote:
>
Quote:
I would now like to only show a certain number of the news articles.
Say the latest five.
>
Certain databases allow you to use a "LIMIT" clause to limit the number of
results returned by a SELECT query. Because this is not part of the
official SQL standard, the exact syntax varies between databases. Here are
some examples:
>
MySQL:
SELECT * FROM articles ORDER BY created_date DESC LIMIT 5;
>
PostgreSQL:
SELECT * FROM articles ORDER BY created_date DESC LIMIT 5;
>
Microsoft SQL Server:
SELECT TOP 5 * FROM articles ORDER BY created_date DESC;
>
Thanks for that I knew there was something simple I was missing.


Closed Thread