Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5842897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:04:45+00:00 2026-05-22T12:04:45+00:00

I’m having trouble getting the count of rows from a specific table on two

  • 0

I’m having trouble getting the count of rows from a specific table on two separate conditions for rows from a main table :

Mainly, I’ve got a table for players, with simple informations, like playername and id.
I’ve got also a table with action, where each player is listed with the type of the action (goal or assist). I want to get all players with a column counting their goals, and another counting their assists. Of course, I want the lazy players, whom haven’t score any goals, or made any assists.

For now I came up with this request, but it only gives me the best players, whereas i want all players. Please light my mind 😉

SELECT
  `p`.`playername`, 
  COUNT(`a1`.`actionid`) AS `goals`, 
  COUNT(`a2`.`actionid`) AS `assists`

FROM `Player` AS `p`

LEFT JOIN `Actions` AS `a1` 
ON `a1`.`id` = `p`.`id`

LEFT JOIN `Actions` AS `a2` 
ON `a2`.`id` = `p`.`id`

WHERE `a1`.`type` = 'goal'
  AND `a2`.`type` = 'assist'

GROUP BY  
  `a1`.`userId`,  
  `a2`.`userId` 

LIMIT 0 , 30
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:04:45+00:00Added an answer on May 22, 2026 at 12:04 pm

    I noticed in your query that you’re joining your actions table on a condition which references a table aliased as u of which you don’t have any table assigned. So, I think your query is probably throwing a MySQL error. Also, in your join conditions you are referencing the Actions table’s id field, which I would expect to be, under normal convention, a primary auto-increment key. You may be using it as the player id, I’m unsure, but to be more verbose I changed it to read player_id so you and other would know exactly what was going on in the query below. Under the assumption that you edited your query to be “easier to read” in the SO editor and didn’t test it, I tweaked it to what I thought would be more accurate.

    When joining tables and running a COUNT on the select, you can only count the total number of rows returned. You can’t COUNT based on the number of rows returned for a single table in the JOIN. If you absolutely must use COUNT, you have to run more than 1 query (1 for each COUNT you need to run) or use sub-queries that run the COUNT for the particular condition you’re looking for.

    This query is possible to run as a single query, but COUNT is not the solution. You use a combination of CASE and SUM. We run a CASE..WHEN clause that tests each row to see if the action that has been joined is a goal or an assist. If it is a goal, we give the goals column a value of 1, and if it’s an assist we give the assists column a value of 1. Then we run an aggregate function (SUM) to add up all the 1s for goals and assists, and GROUP BY the player id. This gives you a single row for each player with the total # of goals and assists they earned even for the players that earned 0.

    SELECT 
        p.payername, 
        SUM(CASE a.type WHEN 'goal' THEN 1 ELSE 0 END) goals, 
        SUM(CASE a.type WHEN 'assist' THEN 1 ELSE 0 END) assists
    FROM Player p
    LEFT JOIN Actions a ON a.player_id = p.id
    GROUP BY p.id
    LIMIT 0, 30
    

    I hope that helps you out and makes sense to you. You can check out the documentation for CASE to get some more insight if you’re still confused.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.