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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:28:32+00:00 2026-06-14T17:28:32+00:00

I have a mysql db with three tables student student_intervention intervention details I’m trying

  • 0

I have a mysql db with three tables

student
student_intervention
intervention details

I’m trying to do a pivot table view that shows all the students and has columns for each intervention type totalling up the different types of intervention for each student.

So far I have

 SELECT  t.`first_name`, t.`last_name`, t.`student_id`,
        Count(IF(t.`intervention_details_id` = 1, 1, null)) AS Intervention1,
        Count(IF(t.`intervention_details_id` = 0, 1, null)) AS Intervention2
 FROM  (
            SELECT student.`student_id`,  student.`first_name`,
                    student.`last_name`,
                    `student_intervention`.`intervention_details_id`
            FROM student, student_intervention 
            WHERE student_intervention.student_id = student.`student_id`
            ) t 
 GROUP BY t.student_id

This works but it only shows data for students who have an intervention. I want a full list of students including those without an intervention. I think I need a JOIN but cannot figure out the right one.

Can anyone help?

  • 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-06-14T17:28:33+00:00Added an answer on June 14, 2026 at 5:28 pm

    use LEFT JOIN instead

    SELECT  a.`student_id`,  
            a.`first_name`,
            a.`last_name`,
            SUM(IF(COALESCE(b.`intervention_details_id`,0) = 1, 1, 0)) Intervention1,
            SUM(IF(COALESCE(b.`intervention_details_id`,0) = 0, 1, 0)) Intervention2
    FROM    student a
            LEFT JOIN student_intervention b
                ON b.student_id = a.`student_id`
    GROUP BY a.`student_id`, a.`first_name`, a.`last_name`
    

    if you want prepared statement

    SET @sql = NULL;
    
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'SUM(CASE WHEN COALESCE(b.intervention_details_id ,0) = ',
          COALESCE(b.intervention_details_id ,0),
          ' THEN 1 ELSE 0 END) AS ',
          COALESCE(b.intervention_details_id ,0)
        )
      ) INTO @sql
    FROM    student a 
            LEFT JOIN student_intervention b
                ON b.student_id = a.student_id;
    
    SET @sql = CONCAT('SELECT  a.student_id , a.first_name , a.last_name , ', @sql, ' 
                       FROM     student a
                                LEFT JOIN student_intervention b
                                    ON b.student_id = a.student_id 
                        GROUP BY a.student_id , a.first_name , a.last_name');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three mysql table from same database Db1. Three tables have following columns.
I have a MySQL query that: gets data from three tables linked by unique
I have three mysql tables that I would like to extract some information from,
I have a Mysql table where it combines three tables. This table stores some
I have a MySQL Left Join problem. I have three tables which I'm trying
I am stuck with a mysql query. I have three tables: Table Name :
I have three mysql tables items =========== id title items_in_categories ============================ id item_id category_id
I'm using MySQL I have three tables: accounts { account_id, username } account_ips {
I have three tables in a Mysql database - countries, cities and hotels. Their
Using MySQL, I have three tables: projects : ID name 1 birthday party 2

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.