0

I am wondering how I can make this work, I tried:

$query = "SELECT author, SUM(likes) FROM posts WHERE author = '$usern' GROUP BY likes order by SUM(likes) + 0 desc"; 

$result = mysql_query($query) or die(mysql_error());

But it just gives me number of likes of first post, not from all posts in-one. So, I need to get all likes from all posts where username is $usern

Database rows: id, likes, author, date

I need to output one number, e.g. 50 if the author has 5 posts and on every post 10 likes

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Mc Filip
  • 19
  • 4
  • If you get rid of the GROUP BY, you get what you want. You only need GROUP BY if you want a query that performs the calculation for all authors. – Glorfindel Jul 12 '15 at 13:14
  • Hello, thanks for comment, I dont need from all authors, I need just from author named '$usern', I need from all posts where is author named like that, thanks. – Mc Filip Jul 12 '15 at 13:15

2 Answers2

1

If you really only want the total number of likes for $usern, this simple query will already suffice:

$query = "SELECT SUM(likes) FROM posts WHERE author = '$usern'";

You only need a GROUP BY only if you want to retrieve this information for multiple authors at once:

$query = "SELECT author, SUM(likes) FROM posts GROUP BY author ORDER BY SUM(likes) + 0 DESC"; 
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
0
SELECT `author`, SUM(`likes`) FROM `posts` WHERE `author` = $user;

this query will show you how many likes have got $user under all his posts

Dm3Ch
  • 621
  • 1
  • 10
  • 26