1

hi guys im using this MYSQL query to get the top uploader users and the total of thier images views in the current month :

    $users = DB::query("SELECT * ,
  COUNT(p.id) as numPics,
  SUM(p.views) as totalViews

FROM
  images p 
INNER JOIN
  users u
ON
  p.user_id = u.id
 WHERE 
 p.created_at >= \"$current_month\"
GROUP BY p.user_id
ORDER BY totalViews DESC LIMIT 10");

the trouble that the totalViews return the total of views of all pictures , what i want is to get the total of views of the pics uploaded in the current month . thanks .

6
  • can you put the direct where can i find the solution bcz the full code works fine except what i posted . Commented May 6, 2013 at 13:37
  • did u try subquery pls try u might get result Commented May 6, 2013 at 13:37
  • can put some exmaple plz ? Commented May 6, 2013 at 13:38
  • SELECT * , COUNT(p.id) as numPics,(select SUM(p.views) as totalViews where user_id=p.id) try something like this Commented May 6, 2013 at 13:42
  • Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE p.created_at >= "2013-05-01 23:59:59") Commented May 6, 2013 at 14:59

4 Answers 4

1

Use MONTH(NOW()) in mysql for getting data for current month

p.created_at >= MONTH(NOW())
Sign up to request clarification or add additional context in comments.

7 Comments

While this is good advice, it doesn't directly answer the question. Consider expanding your answer to identify the OP's current problem.
im using this structure : $current_month = date('Y-m-d', strtotime("first day of this month")) . ' 23:59:59';
what is the data type of column created_at?
@Hamza: In mysql itself there is func for getting current month. Then why should you go for getting current month using php and implementing in query?
Hmm, would it be this simple: change strto*time* in strto*date*?
|
0

change the group by statement and the select clause to this

SELECT * ,
  (SELECT COUNT(*) FROM images where id = p.id) as numPics,
  SUM(p.views) as totalViews

FROM
  images p 
INNER JOIN
  users u
ON
  p.user_id = u.id
 WHERE 
 p.created_at >= \"$current_month\"
GROUP BY p.user_id, p.id
ORDER BY totalViews DESC LIMIT 10"

Comments

0

You need to use extract method

"SELECT * ,
  (SELECT COUNT(*) FROM images where id = p.id) as numPics,
  SUM(p.views) as totalViews

FROM
  images p 
INNER JOIN
  users u
ON
  p.user_id = u.id
 WHERE 
EXTRACT(YEAR_MONTH FROM p.created_at) >= EXTRACT(YEAR_MONTH FROM \"$current_month\")
GROUP BY p.user_id, p.id
ORDER BY totalViews DESC LIMIT 10"

The name of variable is miss-leading i.e $current_month , but your comments says it has a time stamp value e.g "2013-05-01 23:59:59"

Comments

0

This query looks like a perfect example to make it a little more generic. When you add an upper limit for created_at you can use the same query also to find the top of last month (or any other month).

$lower_limit = date('Y-m-d', strtotime('first day of this month'));
$upper_limit = date('Y-m-d', strtotime('first day of next month'));

I do assume that you're using PDO prepared statements, so I'll just provide the SQL.

SELECT u.*,
  COUNT(p.id) as numPics,
  SUM(p.views) as totalViews
FROM
  images p 
INNER JOIN
  users u
ON
  p.user_id = u.id
WHERE 
  p.created_at >= :lower_limit
AND p.created_at < :upper_limit
GROUP BY p.user_id
ORDER BY totalViews DESC
LIMIT 10

Note that this does not selecting any images. The result set should be consistent, and when you group by user_id you have no control over the image data in the result set.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.