how to select a record from one table and count the number of which appears another table and count it

sureone

I have below tow table table 1: The teacher table

teacher_id   teacher_name
1            xx
2            yy
3            zz

table 2: the student table

stu_id       stu_name     tearcher1_id     teacher2_id    tearcher3_id
1            aa           1                2 
2            bb           2                3
3            cc           1

I want to get a list by one sql statement from the teachers included the count of who appears in student table as below:

teacher_id      teacher_name          num_selected_by_stu
1               xx                    2
2               yy                    2
3               zz                    1

I have tried below sql but seems not work,

    select * from teatcher t1 
    left join (
    select stu_id,tearcher1_id,tearcher2_id,tearcher3_id,count(stu_id) as num_selected_by_stu from student 
    group by stu_id,tearcher1_id,tearcher2_id,tearcher3_id) t2 
    ON ( t2.teacher1_id=t1.teacher_id or t2.teacher2_id=t1.teacher_id or t2.teacher3_id=t1.teacher_id)

and,anyone can help?

schurik
SELECT 
  teacher_id
  teacher_name
  NVL(num1, 0) + NVL(num2, 0) + NVL(num3,0) as num_selected_by_stu
FROM 
  teacher t
  left outer join ( SELECT count(*) as num1, tearcher1_id FROM student group by tearcher1_id ) t1 on t1.tearcher1_id = t.tearcher_id
  left outer join ( SELECT count(*) as num2, tearcher2_id FROM student group by tearcher2_id ) t2 on t2.tearcher2_id = t.tearcher_id
  left outer join ( SELECT count(*) as num3, tearcher3_id FROM student group by tearcher3_id ) t3 on t3.tearcher3_id = t.tearcher_id
;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select from one table and count from another

From Dev

join count from one table to select from another - mysql

From Dev

MySQL query - select from one, count from another table

From Dev

select from one table, count from another where id is not linked

From Dev

How to combine records from different table into one record set with count

From Dev

MYSQL select from table and count from another

From Dev

How to add one column which is the count(*) from the table?

From Dev

How do I count the number of times each keyword from a table appears in a table of phrases?

From Dev

SQLite select and count from another table with like

From Dev

Select * as well as count/sum from another table

From Dev

How to count rows that got copied from one Hive table to another

From Dev

MySQL count of count, use result from one table with another

From Dev

get all record with number of one to many table count

From Dev

get all record with number of one to many table count

From Dev

Display the details from one table and count from another table

From Dev

How to select from one dbms_sql.number_table into another

From Dev

Select rows from a table satisfying criteria for all rows in a child table which have at least one record in another table

From Dev

How use count for value from another table

From Dev

How to count data from another table

From Dev

How to insert record from one table to another?

From Dev

How to count rows from another table to affect another table

From Dev

SQL - Select record count from column value in separate table

From Dev

SQL query to select row from one table which is not in another table

From Dev

How to select all records from one table that do not exist in another table but return NULL in the record that do not exist

From Dev

MYSQL join one colum from one table to two count() in another

From Java

SQL query INSERT SELECT COUNT from one table to another row by row

From Dev

How to select count from Table A with conditions from Table B

From Dev

Count Row from another table

From Dev

Filter by count from another table

Related Related

  1. 1

    Select from one table and count from another

  2. 2

    join count from one table to select from another - mysql

  3. 3

    MySQL query - select from one, count from another table

  4. 4

    select from one table, count from another where id is not linked

  5. 5

    How to combine records from different table into one record set with count

  6. 6

    MYSQL select from table and count from another

  7. 7

    How to add one column which is the count(*) from the table?

  8. 8

    How do I count the number of times each keyword from a table appears in a table of phrases?

  9. 9

    SQLite select and count from another table with like

  10. 10

    Select * as well as count/sum from another table

  11. 11

    How to count rows that got copied from one Hive table to another

  12. 12

    MySQL count of count, use result from one table with another

  13. 13

    get all record with number of one to many table count

  14. 14

    get all record with number of one to many table count

  15. 15

    Display the details from one table and count from another table

  16. 16

    How to select from one dbms_sql.number_table into another

  17. 17

    Select rows from a table satisfying criteria for all rows in a child table which have at least one record in another table

  18. 18

    How use count for value from another table

  19. 19

    How to count data from another table

  20. 20

    How to insert record from one table to another?

  21. 21

    How to count rows from another table to affect another table

  22. 22

    SQL - Select record count from column value in separate table

  23. 23

    SQL query to select row from one table which is not in another table

  24. 24

    How to select all records from one table that do not exist in another table but return NULL in the record that do not exist

  25. 25

    MYSQL join one colum from one table to two count() in another

  26. 26

    SQL query INSERT SELECT COUNT from one table to another row by row

  27. 27

    How to select count from Table A with conditions from Table B

  28. 28

    Count Row from another table

  29. 29

    Filter by count from another table

HotTag

Archive