Jump to content

get last 10 records in database

- - - - -

  • Please log in to reply
9 replies to this topic

#1
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
hi guys

i want get last 10 records in database

this is my query

SELECT * FROM `mytable` WHERE `field` = '21' ORDER BY `id` ASC LIMIT 0, 10

but this query get top 10 records

i need get last 10 record with ASC by id

help me please

thank you

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What fields are in mytable? There are two ways to do this:
1) do a SELECT COUNT(*) first, and use that to determine how many records to skip
2) do a SELECT * FROM (SELECT * FROM `mytable` WHERE field = '21' order by `id` DESC LIMIT 0,10) order by `id` ASC
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
thank you

i have a error

my fields are(id, field, name, session, date)

you said use this query?

SELECT * FROM (SELECT * FROM `mytable` WHERE field = '21' order by `id` DESC LIMIT 0,10) order by `id` ASC

Syntax error or access violation: 1248 Every derived table must have its own alias

help more please

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
"Syntax error or access violation: 1248 Every derived table must have its own alias" - Google Search
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
lol33d

lol33d

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
please query

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
but the last 10 while sorting ASC must be the 10 first when sorting DESC? So just limit 10 on DESC.. or?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
The double query is just to resort it to ascending.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
amrosama

amrosama

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 8,674 posts
why not just do :amr:
SELECT * FROM `mytable` WHERE `field` = '21' ORDER BY `id` DESC LIMIT 0, 10
Am I missing something?
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
He cares about the order, for some reason.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
Errr Winged, I think he just wants to get the last 10 results in any order.
You can tell by his grammar that he is really confused...
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users