Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 select last 20 rows

Author  Topic 

dbusher
Starting Member

7 Posts

Posted - 2007-06-06 : 17:15:24
I am very new to db and sql. I have a populated table and would like to run a query that brings up the last 20 rows. Here is what I have now...do I need a WHERE statement?

SELECT

d.cache_diff AS $<scachediff>,
d.cache_name AS $<scachename>,
d.cache_type AS $<scachetype>,
d.cache_last_found AS $<cachelastfound>,
d.cache_id AS $<cache_view_link>,
d.cache_id AS $<log_find_link>

FROM geocaches d

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-06-06 : 17:17:55
There is no first/last concept in SQL Server unless you specify an ORDER BY clause. So use a TOP 20 in your SELECT with an ORDER BY whatever column.

SELECT TOP 20
d.cache_diff AS $<scachediff>,
d.cache_name AS $<scachename>,
d.cache_type AS $<scachetype>,
d.cache_last_found AS $<cachelastfound>,
d.cache_id AS $<cache_view_link>,
d.cache_id AS $<log_find_link>
FROM geocaches d
ORDER BY <column>


Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

dbusher
Starting Member

7 Posts

Posted - 2007-06-06 : 20:11:41
I simplified the query but still can't get this to work. Not sure what I am doing wrong

SELECT TOP 10
log_id
FROM logvisit
ORDER BY cache_id DESCENDING

Here is the error

SQL query: Documentation

SELECT TOP10log_id
FROM logvisit
ORDER BY cache_id DESCENDING
LIMIT 0 , 30

MySQL said: Documentation
#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '10 log_id FROM logvisit ORDER BY cache_id DESCENDING
LIMIT
Go to Top of Page

dbusher
Starting Member

7 Posts

Posted - 2007-06-06 : 20:20:40
i think its because i am using mysql...the following query gets the first 10 logs...now I just need to figure out how to get the last 10.

SELECT
log_type,
log_owner_name,
the_log
FROM logvisit
LIMIT 10
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-06-07 : 00:55:54
Put an ORDER BY col DESC in the query to sort in descending order, then the 10 you get will be the last 10.

This is a SQL Server forum, for MySql you should rather go to forums.mysql.com as the MySql experts are over there.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-07 : 10:07:18
Post your question at www.mysql.com

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -