If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > How to select 5 results only ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-26-04, 05:56
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
How to select 5 results only ?

Hi, thakns for reading my post

What is the sql, command to select only 5 results from my table ?

I mean, when I have a table with 100 replies to one topic, and I want to select only 5 of them.

Thanks a lot
Reply With Quote
  #2 (permalink)  
Old 07-26-04, 06:44
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
Oracle:

Select columns
from table
where rownum <= 5

SQL Server 2000:

Select top 5 columns
from table
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #3 (permalink)  
Old 07-26-04, 08:01
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
Thanks a lot, gonna try it at once.
Reply With Quote
  #4 (permalink)  
Old 07-26-04, 08:07
laals laals is offline
Registered User
 
Join Date: Jul 2004
Posts: 13
Sql

or u have to use the LIMIT statement
optional LIMIT OFF
Reply With Quote
  #5 (permalink)  
Old 07-26-04, 08:07
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
Hmm, this doesnt works on my sql server

I tried : "select * from Table where rownum=<5";
and also : "select top 5 colums from table";
Reply With Quote
  #6 (permalink)  
Old 07-26-04, 08:10
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
Quote:
Originally Posted by laals
or u have to use the LIMIT statement
optional LIMIT OFF

How to use Limit statement please ?
Thanks
Reply With Quote
  #7 (permalink)  
Old 07-26-04, 08:35
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
This query will work in MS SQL Server 2000:
Code:
Select top 5 *
from table
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.

Last edited by r123456; 07-26-04 at 08:38.
Reply With Quote
  #8 (permalink)  
Old 07-26-04, 09:08
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
Still doesn't work :/

Code:
$sql="Select top 2 * from clen";
$res=mysql_query($sql);
while($line=mysql_fetch_assoc($res)) echo($line[0]);
gives :
Code:
 Supplied argument is not a valid MySQL result resource in /home/... /html/lib.php on line 258
Reply With Quote
  #9 (permalink)  
Old 07-26-04, 09:57
mhall84 mhall84 is offline
Registered User
 
Join Date: Jul 2004
Posts: 3
SELECT * FROM yourtable LIMIT 2

Looks like you are using MySQL, not Microsoft SQL Server. the TOP statement works in Microsoft SQL Server but I don't think it is in MySQL as a valid statement.
Reply With Quote
  #10 (permalink)  
Old 07-26-04, 10:06
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
Yes that's it
It works
Thanks a lot
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On