How could I use join query to find the intersection

user3675188

I want to find the users who haven't make any order

My idea is that, Get all user ids and substract the unique ids in orders table

How could I convert it in MySQL query syntax.

mysql> select * from users;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

mysql> select * from orders;
+------+---------+--------+
| id   | user_id | amount |
+------+---------+--------+
|    1 |       1 |  10.00 |
|    1 |       2 |   5.00 |
+------+---------+--------+
2 rows in set (0.00 sec)
1000111

The idea is to make make a left join between users (left table) and orders table.

Then from this joined table you need to filter those records which don't have any order. So in this case orders.id would be NULL.

SELECT 
users.id
FROM users
LEFT JOIN orders
ON users.id = orders.user_id
WHERE orders.id IS NULL

Visual Understanding:

enter image description here

SQL Joins explained better here.

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 could I find the area of the intersection of line and contour

From Dev

How could I use case within join and union?

From Dev

How do I find the intersection of 2 sets?

From Dev

How do I find the intersection of 2 sets?

From Dev

How should I use Rails to index and query a join table?

From Dev

How to do this query in MySQL which one should I use using left join or right join or inner join?

From Dev

How could I join row in datagridview?

From Dev

How do I use `Explain` with a `find_by_sql` query?

From Dev

How do I use `Explain` with a `find_by_sql` query?

From Dev

error :Could not find an implementation of query for source type datatable and join not found while trying to join two datatables

From Dev

JIRA search query - how could I use 'status changed' to have 'after startOfDay' + fixed HH:mm?

From Dev

How could I form a tsql query with 'and' and 'not' for a result

From Dev

How could I form a tsql query with 'and' and 'not' for a result

From Dev

How do I find the intersection of two line segments?

From Dev

How can I find the intersection of a list and a nested list?

From Dev

How could I use Bash to find 2 bytes in a binary file, increase their values, and replace?

From Dev

How could I find the Plan command?

From Dev

How can I select all fields in a join that I can use later with other query?

From Dev

How do I use the union and intersection on a list of Counters of unknown length?

From Dev

How do I use double dispatch to analyze intersection of graphic primitives?

From Dev

How do I use double dispatch to analyze intersection of graphic primitives?

From Dev

How to use JOIN instead of sub query (NOT IN)

From Dev

How to use ORDER BY in a query which is containing JOIN?

From Dev

how to use count with where clause in join query

From Dev

How to use join in my case Postgres query ?

From Dev

How to use join query for 4 tables in rails

From Dev

How to use Spring + Hibernate query for join table

From Dev

How to use sql LEFT JOIN query

From Dev

How to Use Where Condition With Join Query in Laravel

Related Related

  1. 1

    how could I find the area of the intersection of line and contour

  2. 2

    How could I use case within join and union?

  3. 3

    How do I find the intersection of 2 sets?

  4. 4

    How do I find the intersection of 2 sets?

  5. 5

    How should I use Rails to index and query a join table?

  6. 6

    How to do this query in MySQL which one should I use using left join or right join or inner join?

  7. 7

    How could I join row in datagridview?

  8. 8

    How do I use `Explain` with a `find_by_sql` query?

  9. 9

    How do I use `Explain` with a `find_by_sql` query?

  10. 10

    error :Could not find an implementation of query for source type datatable and join not found while trying to join two datatables

  11. 11

    JIRA search query - how could I use 'status changed' to have 'after startOfDay' + fixed HH:mm?

  12. 12

    How could I form a tsql query with 'and' and 'not' for a result

  13. 13

    How could I form a tsql query with 'and' and 'not' for a result

  14. 14

    How do I find the intersection of two line segments?

  15. 15

    How can I find the intersection of a list and a nested list?

  16. 16

    How could I use Bash to find 2 bytes in a binary file, increase their values, and replace?

  17. 17

    How could I find the Plan command?

  18. 18

    How can I select all fields in a join that I can use later with other query?

  19. 19

    How do I use the union and intersection on a list of Counters of unknown length?

  20. 20

    How do I use double dispatch to analyze intersection of graphic primitives?

  21. 21

    How do I use double dispatch to analyze intersection of graphic primitives?

  22. 22

    How to use JOIN instead of sub query (NOT IN)

  23. 23

    How to use ORDER BY in a query which is containing JOIN?

  24. 24

    how to use count with where clause in join query

  25. 25

    How to use join in my case Postgres query ?

  26. 26

    How to use join query for 4 tables in rails

  27. 27

    How to use Spring + Hibernate query for join table

  28. 28

    How to use sql LEFT JOIN query

  29. 29

    How to Use Where Condition With Join Query in Laravel

HotTag

Archive