Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > MySQL > simple sql query - help please!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-09-02, 19:25
anne anne is offline
Junior Member
 
Join Date: Mar 2002
Posts: 2
Question simple sql query - help please!

Hello everybody.

I have a table that contains id, name, message, and date.
I would like to pull the 10 last posted messages, but have a hard time doing it. I would also like to take into consideration that ids might not be consecutive as some messages get deleted.

Maybe I am doing it wrong.

This is what I am trying with no success:

select * from messages where id > max(id)-10;

this gives me a ERROR 1111: Invalid use of group function.

Can anyone please please suggest what I am not doing right?

I also tried different variations of the above query with disaster... =(

Thanks a bunch.
Reply With Quote
  #2 (permalink)  
Old 03-09-02, 19:28
anne anne is offline
Junior Member
 
Join Date: Mar 2002
Posts: 2
what i tried:

Right now I get by using one query for getting the max(id), then doing another query for all messages whose ids are greated than int num = max(id) -10;

anne b.
Reply With Quote
  #3 (permalink)  
Old 03-09-02, 21:09
Paul Paul is offline
Administrator
 
Join Date: Feb 2001
Location: NC, USA
Posts: 200
When using the max() function or any other aggregation function, you must include a group by clause.

To get the latest 10 posts you can use the LIMIT clause.
Code:
SELECT * FROM messages ORDER BY date DESC LIMIT 10;
Reply With Quote
  #4 (permalink)  
Old 03-16-02, 06:34
vivekphanse vivekphanse is offline
Registered User
 
Join Date: Mar 2002
Posts: 10
Hi Anne,

I belive u cant' get max(of any column) directly. Yu need to fire the query for it.

Acc. to me it should be, select * from tablename where id > (select max(id) from tablename -10.

But this wont work in mysql as it doesnt support nested querries...

This is what I belive, If i am wrong...correct me please.

Cheers,
vvk
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

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