Jump to content

fun with joins/count()


ifm1989

Recommended Posts

Ok, so I have two databases.

 

One is unlocks. One is unlocks_user.

 

Unlocks has columns like id,bonus1,bonus2, bonus3

 

unlocks_user has columns user_id,unlock_id

 

Im trying to make a function that counts your total bonus1, bonus2, bonus3 based off if you have unlocked it or not. I was able to accomplish this with a while statement, but I feel it would be much more efficient to use the mysql function count().

 

How would I do this?

 

This seems to return incorrect numbers:

 

$result = dbquery("SELECT count(fusion_unlocks.bonus1) AS bonus1

FROM fusion_unlocks,fusion_unlocks_user

WHERE (fusion_unlocks.id = fusion_unlocks_user.unlocks_id)

AND (fusion_unlocks_user.user_id = $user_id)");

Link to comment
Share on other sites

Don't use count() in this case (the function SUM() is used to total things up). I assume bonus1-3 are integers, and if so just add the column values together. Example:

 

SELECT

          fu.bonus1 + fu.bonus2 + fu.bonus3 AS total_bonuses

  FROM

          fusion_unlocks_user fuu

  JOIN

          fusion_unlocks fu

    ON

          fu.id = fuu.unlocks_id

WHERE

          fuu.user_id = $user_id

 

Link to comment
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.