user1260310
user1260310

Reputation: 2227

PHP/MYSQL Most popular query syntax

I am trying to create a function that returns an array of idnumbers (for pages) based on the number of views of the page ranked by number of views. Every time someone views table, there is a new row. So I basically need to count the rows but do so by pageid. I'm thinking it uses count but SQL is not my strong suit. Can anyone suggest the proper way to do this?

table 'views':

id | pageid | 

SELECT COUNT(id) from views WHERE pageid== '22' ORDER BY COUNT(id)

Upvotes: 0

Views: 299

Answers (2)

JoeTomato
JoeTomato

Reputation: 316

I would post this as a comment, but I apparently don't have access to comment yet...

How would I echo Count(id)?

Assign an alias like so:

SELECT pageid, COUNT(id) as viewcount FROM views 
WHERE pageid== '22' 
GROUP BY pageid 
ORDER BY COUNT(id)

Then you can access the row with:

echo $row['viewcount'];

Sorry I can't help with the joins part - I've never had much luck joining tables while using functions like count or group by.

Upvotes: 1

Nico
Nico

Reputation: 473

Try this

SELECT pageid, COUNT(id) FROM views 
WHERE pageid== '22' 
GROUP BY pageid 
ORDER BY COUNT(id)

Upvotes: 4

Related Questions