MySQL join 3 tables and find "missing" rows in one table

bhttoan

I have 3 tables in MySQL and I need to find all instances where a user does not have a specific widget.

Example:

Users
tenant_id user_id user_name
1         1       Bob
1         2       Fred
1         3       John
1         4       Tom

Widgets
tenant_id widget_id widget_name
1         1         Red
1         2         Blue
1         3         Green
1         4         Black

Usage
tenant_id user_id widget_id
1         1       1
1         1       2 
1         1       4
1         2       2
1         2       3
1         2       4
1         3       1
1         3       2
1         3       3
1         3       4
1         4       1
1         4       2
1         4       3

Missing in table three are:
user_name widget_name
Bob       Green
Fred      Red
Tom       Black

The query I am trying to use is:

SELECT
  user_name, widget_name
FROM
  users left join widgets on users.tenant_id=widgets.tenant_id
WHERE 
  NOT EXISTS (
    SELECT 1 FROM usage WHERE user_id = users.user_id AND widget_id = widgets.widget_id
  ) and users.tenant_id=1

When the query completes it brings back a huge list of usernames and widget names but many hundreds more than I expect.

I am not sure if the join is wrong or if I need to do some sort of grouping on the result?

Gordon Linoff

Here is the idea behind this type of query. First generate all possible combinations of users and widgets. Then filter out the ones that exist:

select u.user_name, w.widget_name
from users u join
     widgets w
     on u.tenant_id = w.tenant_id
where not exists (select 1
                  from usage us
                  where us.user_id = u.user_id and us.widget_id = w.widget_id and
                        us.tenant_id = u.tenant_id
                 ) and
      u.tenant_id = 1;

I think your logic is missing the tenant_id in the usage table. However, I'm not sure that makes a big difference.

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 join two table rows in one table

From Dev

left join of 3 tables according to one table

From Dev

MySQL JOIN one of the rows from second table

From Dev

MySQL Join 3 relative tables in one statement

From Dev

MySQL join table only to find missing values from previous join

From Dev

MySQL join table only to find missing values from previous join

From Dev

Deleting rows from 3 tables in MySQL table

From Dev

Join two tables and show Columns of one table as Rows

From Dev

mysql query to join 3 tables having no association in one of the tables

From Dev

Join multiple rows of one table on one record (Codeigniter 3)

From Dev

MySQL JOIN of 3 tables

From Dev

Join 3 tables and Obtaining last update date from one table

From Dev

MySQL, Join two rows from one table and ignore doublons

From Dev

MYSQL : Mix data from 3 tables and JOIN with another table

From Dev

MySQL set column value from another table (JOIN 3 tables)

From Dev

MYSQL : Mix data from 3 tables and JOIN with another table

From Dev

MySQL join on one table

From Dev

Find total number of rows using data from 3 tables, MySQL

From Dev

MySQL JOIN Query Help : join two tables with distinct right table rows ordered in descending order

From Dev

Join MySQL tables on bad table

From Dev

Join MySQL tables on bad table

From Dev

related tables, how to find records if there are missing rows

From Dev

Mysql - Finding missing dates with multiple join tables

From Dev

MySQL - How to use (LEFT) JOIN to join two tables via one table together?

From Dev

MySQL - How to use (LEFT) JOIN to join two tables via one table together?

From Dev

Mysql join table and count rows

From Dev

mysql join 3 tables by id

From Dev

join 3 mysql tables with condition

From Dev

Inner join with 3 tables MySql

Related Related

  1. 1

    mysql join two table rows in one table

  2. 2

    left join of 3 tables according to one table

  3. 3

    MySQL JOIN one of the rows from second table

  4. 4

    MySQL Join 3 relative tables in one statement

  5. 5

    MySQL join table only to find missing values from previous join

  6. 6

    MySQL join table only to find missing values from previous join

  7. 7

    Deleting rows from 3 tables in MySQL table

  8. 8

    Join two tables and show Columns of one table as Rows

  9. 9

    mysql query to join 3 tables having no association in one of the tables

  10. 10

    Join multiple rows of one table on one record (Codeigniter 3)

  11. 11

    MySQL JOIN of 3 tables

  12. 12

    Join 3 tables and Obtaining last update date from one table

  13. 13

    MySQL, Join two rows from one table and ignore doublons

  14. 14

    MYSQL : Mix data from 3 tables and JOIN with another table

  15. 15

    MySQL set column value from another table (JOIN 3 tables)

  16. 16

    MYSQL : Mix data from 3 tables and JOIN with another table

  17. 17

    MySQL join on one table

  18. 18

    Find total number of rows using data from 3 tables, MySQL

  19. 19

    MySQL JOIN Query Help : join two tables with distinct right table rows ordered in descending order

  20. 20

    Join MySQL tables on bad table

  21. 21

    Join MySQL tables on bad table

  22. 22

    related tables, how to find records if there are missing rows

  23. 23

    Mysql - Finding missing dates with multiple join tables

  24. 24

    MySQL - How to use (LEFT) JOIN to join two tables via one table together?

  25. 25

    MySQL - How to use (LEFT) JOIN to join two tables via one table together?

  26. 26

    Mysql join table and count rows

  27. 27

    mysql join 3 tables by id

  28. 28

    join 3 mysql tables with condition

  29. 29

    Inner join with 3 tables MySql

HotTag

Archive