How do I count number of rows in a table in mysql ?

  • dann123
  • Novice
  • Novice
  • No Avatar
  • Joined: Dec 04, 2010
  • Posts: 17
  • Status: Offline

Post December 6th, 2010, 11:14 am

I want to know how can we count no. of rows in a given table through a single query if there is no candidate key?
And how do I retrieve last ten rows using another query ?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post December 6th, 2010, 11:14 am

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Joined: May 22, 2004
  • Posts: 3415
  • Loc: Richland, WA
  • Status: Offline

Post December 6th, 2010, 12:34 pm

You can use the COUNT function in the select field for finding total rows, and for returning that last 10 rows you can use LIMIT.

Code: [ Select ]
SELECT COUNT(*) FROM table

Code: [ Select ]
SELECT * FROM table ORDER BY field LIMIT 10


For the last one field is some value that has an order to it, such as an ID or insert time.
#define NULL (::rand() % 2)
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post December 6th, 2010, 10:10 pm

SQL Code: [ Select ]
SELECT * FROM `table` ORDER BY `field` DESC LIMIT 10


DESC for the last 10
ASC for the first 10
"Bring forth therefore fruits meet for repentance:" Matthew 3:8

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 214 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.