How to join Count(*) columns with another column in Mysql

invincibles04

I have a table like this

The first table is the "Appointments" table with 'doctor_id' the second table is the "Doctors" table also with 'doctor_id'. The duplicate values from the appointments table represents how many appointment each doctor has. for example the doctor_id(50) has 6 appointments.

doctor_id(Appointments)   |  doctor_id(Doctors)
          50              |         50
          50              |         51
          50              |         52
          52              |         53
          50              |
          50              |
          52              |
          53              |
          50              |

Now, my question is how would I show this table below (ps excluding the doctor id '51)

doctor_id (Doctors) |  Count
        50          |    6
        52          |    2
        53          |    1

This what i have tried so far, i have managed to work out how many doctors have appointments using :

SELECT COUNT(*) FROM appointment 
WHERE doctor_id > 50

I have executed other queries as well but with no luck.

spencer7593

Assuming doctor_id is unique in the doctors table...

SELECT d.doctor_id        AS doctor_id
     , COUNT(a.doctor_id) AS appointment_count
  FROM doctors d
  LEFT
  JOIN appointments a
    ON a.doctor_id = d.doctor_id
 GROUP BY d.doctor_id

To get the count by doctor_id, you need to GROUP BY the doctor_id. You don't have to include doctor_id in the SELECT list, but if you only return the COUNT(), you'd just get a list of counts:

 appointment_count
 -----------------
                 6
                 0
                 2
                 1

With no indication of which appointment_count goes with which doctor_id. So, the usual pattern is to include what you GROUP BY in the SELECT list.

In order to return the 0 count for doctor 51, we want an outer join to the appointments table, so the row from the doctors table is returned even when there is no match.


If you only want rows for doctors that have one or more appointments, then you can just query the appointments table. (This query won't return any "zero" count for a doctor.)

SELECT a.doctor_id
     , COUNT(1) AS appointment_count
  JOIN appointments a
 GROUP BY a.doctor_id

Again, the GROUP BY clause is necessary to get a count of rows for each distinct value of doctor_id. That effectively collapses the rows for each doctor_id into a single row. The aggregate function operates on the "group" of rows.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL query with COUNT and join column from another table

From Dev

Join a columns count from another table

From Dev

How to select a column in a left join if there is 2 columns with the same name ? [MySql]

From Dev

Sum of two columns with join and group by another column

From Dev

Sum columns and count different values of another column

From Dev

Count distinct multiple columns for each another column

From Dev

Sum columns and count different values of another column

From Dev

MySQL Group by column, then count by another column

From Dev

MySQL Group by column, then count by another column

From Dev

MySQL - Join & Count rows from another table

From Dev

MySQL join and count according to its column value

From Dev

MySQL join and count according to its column value

From Dev

MySQL JOIN different tables' columns in one column

From Dev

MySQL LEFT JOIN Count column Join 3 tables

From Dev

MySQL CASE with COUNT query and adding another column

From Dev

How to join table with another that have count?

From Dev

How to count column in inner join query

From Dev

How to join two tables on two columns normally, but if one of columns contain null, then result must contain rows that match another column only?

From Dev

Mysql: how to join 2 columns from same table and use another table for reference

From Dev

Select distinct column and then count 2 columns that relate to that column in MySQL

From Dev

Join two columns in one table to a column in another reference table

From Dev

Optimizing SQL join single column with multiple columns in another table

From Dev

Join column from a table with concat columns from another one

From Dev

Join two columns in one table to a column in another reference table

From Dev

SQL join on columns of same table with MAX on another column

From Dev

Join all multiple columns to the same column of another table

From Dev

How to get DISTINCT column and COUNT of column in MySQL?

From Dev

How to get DISTINCT column and COUNT of column in MySQL?

From Dev

How to join table with prefix column to another two table in PHP MySQL or Laravel

Related Related

  1. 1

    MySQL query with COUNT and join column from another table

  2. 2

    Join a columns count from another table

  3. 3

    How to select a column in a left join if there is 2 columns with the same name ? [MySql]

  4. 4

    Sum of two columns with join and group by another column

  5. 5

    Sum columns and count different values of another column

  6. 6

    Count distinct multiple columns for each another column

  7. 7

    Sum columns and count different values of another column

  8. 8

    MySQL Group by column, then count by another column

  9. 9

    MySQL Group by column, then count by another column

  10. 10

    MySQL - Join & Count rows from another table

  11. 11

    MySQL join and count according to its column value

  12. 12

    MySQL join and count according to its column value

  13. 13

    MySQL JOIN different tables' columns in one column

  14. 14

    MySQL LEFT JOIN Count column Join 3 tables

  15. 15

    MySQL CASE with COUNT query and adding another column

  16. 16

    How to join table with another that have count?

  17. 17

    How to count column in inner join query

  18. 18

    How to join two tables on two columns normally, but if one of columns contain null, then result must contain rows that match another column only?

  19. 19

    Mysql: how to join 2 columns from same table and use another table for reference

  20. 20

    Select distinct column and then count 2 columns that relate to that column in MySQL

  21. 21

    Join two columns in one table to a column in another reference table

  22. 22

    Optimizing SQL join single column with multiple columns in another table

  23. 23

    Join column from a table with concat columns from another one

  24. 24

    Join two columns in one table to a column in another reference table

  25. 25

    SQL join on columns of same table with MAX on another column

  26. 26

    Join all multiple columns to the same column of another table

  27. 27

    How to get DISTINCT column and COUNT of column in MySQL?

  28. 28

    How to get DISTINCT column and COUNT of column in MySQL?

  29. 29

    How to join table with prefix column to another two table in PHP MySQL or Laravel

HotTag

Archive