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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:19:48+00:00 2026-05-26T21:19:48+00:00

I am trying to nest in SQL statements that are trying to pull data

  • 0

I am trying to nest in SQL statements that are trying to pull data from two tables, counting rows from player_id, the rows from 1st_assist and 2nd_assist, displaying in 3 columns accordingly.

Here is my code.

SELECT 

     last_name, 

     (SELECT COUNT (*) FROM goals WHERE player_id = 4 OR player_id = 5) 
     AS player_goals, 

     (SELECT COUNT (*) FROM goals WHERE 1st_assist = 4 OR 1st_assist = 5) 
     AS 1st_assists, 

     (SELECT COUNT (*) FROM goals WHERE 2nd_assist = 4 OR 2nd_assist = 5)
     AS 2nd_assists 

FROM people 

WHERE player_id = 4 OR player_id = 5 

GROUP BY player_id
  • 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-26T21:19:48+00:00Added an answer on May 26, 2026 at 9:19 pm

    Try this out:

    SELECT 
        p.last_name,
        COUNT(DISTINCT g.id) AS player_goals,
        COUNT(DISTINCT a1.id) AS 1st_assists,
        COUNT(DISTINCT a2.id) AS 2nd_assists
    FROM
        people p
        LEFT JOIN goals g ON (g.player_id = p.player_id)
        LEFT JOIN goals a1 ON (a1.1st_assist = p.player_id)
        LEFT JOIN goals a2 ON (a2.2nd_assist = p.player_id)
    WHERE
        p.player_id = 4 OR p.player_id = 5
    GROUP BY p.player_id
    

    Tested:

    mysql> select * from people;
    +-----------+-----------+------------+
    | player_id | last_name | first_name |
    +-----------+-----------+------------+
    |         4 | Francis   | Jeff       |
    |         5 | Doe       | John       |
    |         6 | Barratt   | Glen       |
    +-----------+-----------+------------+
    3 rows in set (0.00 sec)
    
    mysql> select * from goals;
    +----+-----------+------------+------------+---------------------+
    | id | player_id | 1st_assist | 2nd_assist | when_made           |
    +----+-----------+------------+------------+---------------------+
    |  1 |         4 |          6 |          5 | 2011-11-07 15:05:31 |
    |  2 |         4 |          5 |       NULL | 2011-11-07 15:05:37 |
    |  3 |         5 |          4 |          6 | 2011-11-07 15:05:45 |
    |  4 |         6 |          4 |          5 | 2011-11-07 15:05:52 |
    |  5 |         4 |          6 |       NULL | 2011-11-07 15:06:05 |
    |  6 |         5 |       NULL |       NULL | 2011-11-07 15:06:15 |
    |  7 |         6 |          4 |       NULL | 2011-11-07 15:06:21 |
    |  8 |         5 |          6 |       NULL | 2011-11-07 15:06:31 |
    |  9 |         4 |       NULL |       NULL | 2011-11-07 15:15:21 |
    | 10 |         4 |          6 |       NULL | 2011-11-07 15:15:27 |
    | 11 |         6 |          5 |          4 | 2011-11-07 15:15:38 |
    +----+-----------+------------+------------+---------------------+
    11 rows in set (0.00 sec)
    
    mysql> SELECT p.last_name, COUNT(DISTINCT g.id) AS player_goals, COUNT(DISTINCT a1.id) AS 1st_assists, COUNT(DISTINCT a2.id) AS 2nd_assists FROM people p LEFT JOIN goals g ON (g.player_id = p.player_id) LEFT JOIN goals a1 ON (a1.1st_assist = p.player_id) LEFT JOIN goals a2 ON (a2.2nd_assist = p.player_id) WHERE p.player_id = 4 OR p.player_id = 5 GROUP BY p.player_id;
    +-----------+--------------+-------------+-------------+
    | last_name | player_goals | 1st_assists | 2nd_assists |
    +-----------+--------------+-------------+-------------+
    | Francis   |            5 |           3 |           1 |
    | Doe       |            3 |           2 |           2 |
    +-----------+--------------+-------------+-------------+
    2 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL query Question, I am trying to nest two separate queries
I am trying to compare data from columns from two different files. I've attempted
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
I am trying to nest divs so that I can make the page that
How do I nest IF statements in Excel 2010? I found my inspiration trying
I'm trying to nest a FormPanel inside another FormPanel. It seems that any field
I'm trying to select all commission data for one of the invoice that will
I am trying to nest multiple if-statements as the following: #!/bin/bash # start_server.sh #
I'm trying to nest two Grid_Views like ... <asp:GridView ID=gv runat=server CellPadding=0 ForeColor=#333333 GridLines=None
I'm trying to nest a form within another using submit_to_remote but it does a

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.