How to get count from another table (zero when no data) using single query in mysql?

aayushi

I have two tables - 'Offered' and 'joined'. I need count of 'offered_id' from joined table and zero when not found. My tables and output required is below. How can I achieve this using single query in mysql ?

TABLE : OFFERED
===============
offered_id    data
-----------   ----
    1         aaaa
    2         bbbb
    3         cccc
    4         dddd
    5         eeee
    6         ffff

TABLE : JOINED
===============
joined_id      offered_id
-----------    ----------
    1               5
    2               2
    3               2
    4               1
    5               3
    6               2
    7               5

OUTPUT REQUIRED
===============
offered_id      data     count(offered_id) from joined table.(0 for no entry)
-----------    -----     ------------------------------------------------
    1           aaaa          1
    2           bbbb          3
    3           cccc          1
    4           dddd          0
    5           eeee          1
    6           ffff          0
NULL

Using sub query you can achieve that...

SELECT OFFERED.offered_id, OFFERED.data, (SELECT COUNT(JOINED.joined_id) FROM JOINED WHERE JOINED.offered_id = OFFERED.offered_id) AS count_joined FROM OFFERED

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get count from another table (zero when no data) using single query in mysql?

From Dev

How do i get data from two tables using single query in mysql

From Dev

How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

From Dev

Missing rows when duplicating data from another table using MySQL

From Dev

Count number of rows from another table in single query

From Dev

How to get count of a particular item from table using mysql

From Dev

Mysql query with count on another table

From Dev

How do I get data from another table in MySQL?

From Dev

How to count data from another table

From Dev

How to get count of two fields from two different table with grouping a field from another table in mysql

From Dev

How to get count of two fields from two different table with grouping a field from another table in mysql

From Dev

MySQL query - select from one, count from another table

From Dev

How to get the information from table to another page using PHP and MYSQL?

From Dev

How can I get the count of multiple table rows storing in different variables using single query?

From Dev

How to get name from another table when using a key in SELECT?

From Dev

looping mysql query to get multiple rows of data from a table and insert into another table

From Dev

How to get the count from a filtered MYSQL query

From Dev

How to return data from two tables only when the column value in one table is same as another table using MySQL?

From Dev

MySQL query with COUNT and join column from another table

From Dev

Selecting SUM+COUNT from one table and COUNT from another in Single query

From Dev

MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

From Dev

How can I select two different values from another table using only a single query?

From Dev

How I get common values from two query in a table and join it with another table in MySql?

From Dev

How to get data from MYSQL child table using checkbox selection?

From Dev

MySQL + Adjusting current query to get additional data from another table (per column)

From Dev

Hibernate HQL query get data from table associated with another table

From Dev

Get count from another table

From Dev

How to get count of data using mysql

From Dev

How to get count of data using mysql

Related Related

  1. 1

    How to get count from another table (zero when no data) using single query in mysql?

  2. 2

    How do i get data from two tables using single query in mysql

  3. 3

    How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

  4. 4

    Missing rows when duplicating data from another table using MySQL

  5. 5

    Count number of rows from another table in single query

  6. 6

    How to get count of a particular item from table using mysql

  7. 7

    Mysql query with count on another table

  8. 8

    How do I get data from another table in MySQL?

  9. 9

    How to count data from another table

  10. 10

    How to get count of two fields from two different table with grouping a field from another table in mysql

  11. 11

    How to get count of two fields from two different table with grouping a field from another table in mysql

  12. 12

    MySQL query - select from one, count from another table

  13. 13

    How to get the information from table to another page using PHP and MYSQL?

  14. 14

    How can I get the count of multiple table rows storing in different variables using single query?

  15. 15

    How to get name from another table when using a key in SELECT?

  16. 16

    looping mysql query to get multiple rows of data from a table and insert into another table

  17. 17

    How to get the count from a filtered MYSQL query

  18. 18

    How to return data from two tables only when the column value in one table is same as another table using MySQL?

  19. 19

    MySQL query with COUNT and join column from another table

  20. 20

    Selecting SUM+COUNT from one table and COUNT from another in Single query

  21. 21

    MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

  22. 22

    How can I select two different values from another table using only a single query?

  23. 23

    How I get common values from two query in a table and join it with another table in MySql?

  24. 24

    How to get data from MYSQL child table using checkbox selection?

  25. 25

    MySQL + Adjusting current query to get additional data from another table (per column)

  26. 26

    Hibernate HQL query get data from table associated with another table

  27. 27

    Get count from another table

  28. 28

    How to get count of data using mysql

  29. 29

    How to get count of data using mysql

HotTag

Archive