Jump to content

Recommended Posts

as a warning, i have no idea what the SQL_CALC_FOUND_ROWS is there for.

 

i assume you receive a "cannot use aggregate functions without the GROUP BY operator" error?  this is because you're trying to run an aggregate function while selecting individual values.

 

if you're specifying a function that uses multiple rows in its calculation (aggregate, such as SUM()), and telling it to grab values from an individual row as well, how does it know which individual row to grab?  assuming the SUM(views) is for a certain "group", you need to group by that column so that it knows that for a given set of the same column value, it should grab the SUM(views) for that set.

 

it would be helpful to see some database structure and hear what you're actually trying to do.

Link to comment
https://forums.phpfreaks.com/topic/63222-solved-count/#findComment-315082
Share on other sites

negatory, they are distinct features, if somewhat subtle.  WHERE will specify what rows to restrict the aggregate function to DURING its calculation, while GROUP BY will tell restrict the rows BEFORE running the calculation.  see this article for more info:

 

http://www.databasejournal.com/features/mysql/article.php/3469351

 

for example, while SUM()ming a dummy column of which there are many for one username, specifying GROUP BY username and WHERE rank=4 will say grab one SUM() of all columns that have the same username, including only those rows whose rank is 4.

Link to comment
https://forums.phpfreaks.com/topic/63222-solved-count/#findComment-315121
Share on other sites

if you want to grab all of the viewers and the sum of their views for a single user, you would group by viewer (because the SUM()s are related to that viewer themselves), and select where the username = 'user'.

 

SELECT `viewer`, SUM(views) AS totalviews FROM `viewedpro` WHERE `username` = '$_GET[user]' GROUP BY `viewer` ORDER BY `time` DESC

 

this will grab each unique viewer of $_GET[user], along with the SUM() of their views.  perhaps i've mistaken what you're trying to grab - your previous query seems pretty diluted.

Link to comment
https://forums.phpfreaks.com/topic/63222-solved-count/#findComment-315138
Share on other sites

Still nStill not working

 

 

$queryj = "SELECT SQL_CALC_FOUND_ROWS `viewer`, `time`, `views`, SUM(views) FROM `viewedpro` WHERE `username` = '$_GET[user]' GROUP BY `viewer` ORDER BY `time` DESC LIMIT 0,5";

 

$doqueryj = mysql_query($queryj);

$tts = number_format($doqueryj['SUM(views)']);

 

Not giving an error but the output is 0

Link to comment
https://forums.phpfreaks.com/topic/63222-solved-count/#findComment-315156
Share on other sites

if you're already pulling the information from the rows, you might as well tally the sum of views manually from the information pulled.  you only save time/resources with aggregate functions when you weren't necessarily going to pull the data that the function operates on.

 

array_sum() will help you out here.

Link to comment
https://forums.phpfreaks.com/topic/63222-solved-count/#findComment-315176
Share on other sites

so are you trying to show the 5 most recent viewers, along with the total sum?  if so, then consolidation of the two queries won't gain you any ground here.  using SQL_CALC_FOUND_ROWS, as far as i know, won't help you much as you have to run a second query to grab those rows found anyway.  there is a bug regarding this feature as well, see the MySQL manual SELECT syntax page for more info.

Link to comment
https://forums.phpfreaks.com/topic/63222-solved-count/#findComment-315190
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.