Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-24-2013, 06:34 AM   PM User | #1
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Counting number of rows

Good morning,

How can I modify my select query to count the number of rows?

Here is my query:
PHP Code:
$sql 'SELECT username, DATE_FORMAT(join_date, "%d/%m/%y") AS join_date_str
         FROM users
         ORDER BY join_date 
         DESC
         LIMIT 5'

I've tried adding COUNT(id); but it only returns 1 row and it overwrites the username. It does count the number of rows correctly though.

I've looked on mysql reference and googled but can't seem to find the answer I am looking for!

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 02-24-2013, 07:24 AM   PM User | #2
Celtboy
Regular Coder

 
Join Date: May 2002
Location: Virginia, USA
Posts: 620
Thanks: 0
Thanked 6 Times in 6 Posts
Celtboy is an unknown quantity at this point
does this not do what you want?
Code:
$sql = 'SELECT username, DATE_FORMAT(join_date, "%d/%m/%y") AS join_date_str, COUNT(*)
         FROM users
         ORDER BY join_date 
         DESC
         LIMIT 5';
I may not understand what you're after.
Celtboy is offline   Reply With Quote
Old 02-24-2013, 07:52 AM   PM User | #3
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
That's exactly how I tried it.

I also tried putting it before the username field but it seems to overwrite my username field and it only prints one row of data. When I take it out I have my desired result. I need to count how many users there are so I can print out the result on another part of my page.

Kind regards,

Lc.
LearningCoder is offline   Reply With Quote
Old 02-24-2013, 09:00 AM   PM User | #4
Celtboy
Regular Coder

 
Join Date: May 2002
Location: Virginia, USA
Posts: 620
Thanks: 0
Thanked 6 Times in 6 Posts
Celtboy is an unknown quantity at this point
Oh. Yeah. once you toss in a COUNT, it aggregates everything into a single row. The solution would be to add a GROUP BY clause...or actually that will probably not give you the correct row count. You'll either need to use a subquery, or join the table on itself.

This isn't exactly elegant (i'd prefer multiple SQL queries) but...

Code:
SELECT username, DATE_FORMAT(join_date, "%d/%m/%y") as join_date_str,
(SELECT COUNT(*) FROM users) AS num_users
FROM users
ORDER BY join_date
DESC
LIMIT 5;
should do it.
Celtboy is offline   Reply With Quote
Users who have thanked Celtboy for this post:
LearningCoder (02-24-2013)
Old 02-24-2013, 09:47 AM   PM User | #5
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
That works great.

Thank you very much.

Kind regards,

LC.
LearningCoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:52 AM.


Advertisement
Log in to turn off these ads.