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 291711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:06:50+00:00 2026-05-12T06:06:50+00:00

I have on table named players , then other tables named tries , conversions

  • 0

I have on table named players, then other tables named tries, conversions, penalties, dropgoals.

I need to get the player_id from players, then count the following:

  • number of tries for player_id in tries
  • number of conversions for player_id in conversions
  • number of penalties for player_id in penalties
  • number of dropgoals for player_id in dropgoals

And this all needs to happen in one go as there are about fifteen players for each game, the site relates to rugby.

I have tried the following, which works:

SELECT players.player_id, players.number, CASE WHEN (COUNT(tries.player_id) = 0) THEN ' ' ELSE COUNT(tries.player_id) END AS nrTries FROM players LEFT JOIN tries ON players.player_id = tries.player_id WHERE players.team_id IS NULL GROUP BY players.player_id ORDER BY players.number

It select all players from the players table and counts their tries, but as soon as I change it to the following it gives me an error:

SELECT players.player_id, players.number, player.name, CASE WHEN (COUNT(tries.player_id) = 0) THEN ' ' ELSE COUNT(tries.player_id) END AS nrTries FROM players, player LEFT JOIN tries ON players.player_id = tries.player_id WHERE players.player_id = player.player_id AND players.team_id IS NULL GROUP BY players.player_id ORDER BY players.number

It gives the following error:

Unknown column 'players.player_id' in 'on clause'

Can someone please help me with this, I have been struggling for days now?

Thanks in advance

// edit:

Hi, all, I feel very stupid now, this code works brilliantly, except that when a player has scored in more than one type, say tries and conversions, or tries and penalties, it miscounts the number of each type, and they are made all the same.

Let’s say my player scored 1 penalty and 3 dropgoals, it outputs as 3 penalties and 3 dropgoals, I can’t figure out what’s wrong.

Here is my query:

SELECT players.player_id, players.number, player.name, player.surname,
CASE WHEN (COUNT(tries.player_id) = 0) THEN '& nbsp;' ELSE COUNT(tries.player_id) END AS nrTries,
CASE WHEN (COUNT(conversions.player_id) = 0) THEN '& nbsp;' ELSE COUNT(conversions.player_id) END AS nrConversions,
CASE WHEN (COUNT(dropgoals.player_id) = 0) THEN '& nbsp;' ELSE COUNT(dropgoals.player_id) END AS nrDropgoals,
CASE WHEN (COUNT(penalties.player_id) = 0) THEN '& nbsp;' ELSE COUNT(penalties.player_id) END AS nrPenalties
FROM players
LEFT JOIN tries ON players.player_id = tries.player_id AND tries.game_id = '$game_id'
LEFT JOIN conversions ON players.player_id = conversions.player_id AND conversions.game_id = '$game_id'
LEFT JOIN dropgoals ON players.player_id = dropgoals.player_id AND dropgoals.game_id = '$game_id'
LEFT JOIN penalties ON players.player_id = penalties.player_id AND penalties.game_id = '$game_id'
LEFT JOIN player ON players.player_id = player.player_id
WHERE players.player_id = player.player_id AND players.team_id IS NULL AND players.game_id = '$game_id'
GROUP BY players.player_id ORDER BY players.number

Please note: $game_id is a PHP variable.
Also: I have included a space between & and nbsp; otherwise it does not get ouputted to SO.

Can someone please point me in the right direction?

  • 1 1 Answer
  • 1 View
  • 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-12T06:06:50+00:00Added an answer on May 12, 2026 at 6:06 am

    I’m a little confused why you stated Cletus had the correct solution, and proceeded to not use it while updating your original question. That said, Cletus’ solution is slightly off, you need COUNT() not SUM(). Try the following:

    SELECT players.player_id,
    (SELECT COUNT(*) FROM tries WHERE player_id = players.player_id) tries,
    (SELECT COUNT(*) FROM penalties WHERE player_id = players.player_id) penalties
    FROM players;
    

    This will return 0’s instead of &nbsp I’d recommend handling that in your application code though. You can add in the CASE mess if you really want to get the &nbsp from mysql.

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

Sidebar

Related Questions

I have a table named stats player_id team_id match_date goal assist` 1 8 2010-01-01
I currently have a page that displays player information from one table named tblplayers.
Lets say I have table named Place with columns: placeId int not null auto_increment,
I have a table named tbl_Subjects_Taken which lists all the subjects taken by the
I have a table named datas and I'm executing a query like this: SELECT
I have a table named size here, something like this: size id | size
I have a table named scores with the columns id and score . I
I have a table named with Sales having the following columns: Sales_ID|Product_Code|Zone|District|State|Distributor|Total_Sales Now i
I have a table named job and is not plural but I was wondering
I have two tables: players (has a team name column) and teams (also has

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.