Jump to content

Recommended Posts

Hi all

 

Wonder if someone can help.

 

I have the following QUERY which contains a COUNT statement

 

SELECT u.id , COUNT(*) AS `total` FROM log l JOIN users u ON l.userid = u.id WHERE u.place = 1 GROUP BY u.id ORDER BY `total` DESC

 

this works great and returns a result such as

 

1   877
2   455
3   232
4   199

 

but is it possible to do a WHERE statement in my COUNT, so it would look something like

 

COUNT(*) WHERE l.type1 = 1 || l.type2 = 1

 

 

but then also include another COUNT statement which would look like

 

COUNT(*) WHERE l.type3 = 1

 

but multiple the above COUNT number by 2, I know it's totally wrong, but something like

 

COUNT(*) WHERE l.type3 = 1 * 2

 

 

but then ADD the numbers from the 1st COUNT and 2nd COUNT together and output them as the

 

ORDER BY `total` DESC

 

value

 

so it would look something like

 

1   ((COUNT 2 * 2) + COUNT 1)
2   ((COUNT 2 * 2) + COUNT 1)
3   ((COUNT 2 * 2) + COUNT 1)
4   ((COUNT 2 * 2) + COUNT 1)

 

Any help would be great, been trying to crack this for the last few hours but having no luck

Link to comment
https://forums.phpfreaks.com/topic/195993-counting-help/
Share on other sites

I managed with

 

SELECT u.id, l.type1,l.type2,l.type3, (select Count(*) from log l WHERE l.type1 = 1 || l.type2 = 1) AS tot1,(select Count(*) from log l WHERE l.type3 = 1) AS tot2 FROM log l JOIN users u ON l.userid = u.id WHERE u.place = 1 GROUP BY u.id

 

to output

 

id   type1   type2   type3   tot1   tot2
1   1           1         1           233   244
2   1           1         1           200   201
3   1           1         1           123   143
4   1           1         1           89      99
5   1           1         1           2        4

 

which is excellent

 

is it possible to add up "tot1" and "tot2" and output them as "total"

Link to comment
https://forums.phpfreaks.com/topic/195993-counting-help/#findComment-1029483
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.