join two table with non-matching rows

overflow

I am having two table.

one table contains customer details(i.e)

id(customerid--primary key),name,email,phonenumber

And other table contains order table(i.e)

id,customerid(foreign key),ordervalue,orderdate

I need to get the list of customers who have not ordered for last one month(i.e) for the month of august. How can i do it.

This is the query i tried

select a.id,a.name,b.order_date from customers a
left join orders b
on a.id = b.customer_id
where b.order_date is null
Ashish Gaur

This query will extract those customers who haven't order past one month from today:

SELECT a.id, a.name
FROM   customers a
WHERE  NOT EXISTS(SELECT *
        FROM   orders b
        WHERE  b.cid = a.id AND 
        orderdate BETWEEN now()- '1 month'::interval
        AND now());

Here is the SQLfiddle

However, if you want to be more precise where you want last month's orders i.e. from 1st of last month to last date of last month then you can use this :

SELECT a.id, a.name
FROM   customers a
WHERE  NOT EXISTS(SELECT *
        FROM   orders b
        WHERE  b.cid = a.id AND 
        to_char(orderdate, 'Mon YYYY') = 
                  to_char(now()- '1 month'::interval, 'Mon YYYY') );

Here is the SQLfiddle

EDIT

Please also have a look at Roman Pekar's answer which is more efficient.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

LEFT JOIN table to find non matching rows, same table

From Dev

How to select non-matching rows from two mysql table

From Dev

How to join two table with non-existing rows

From Dev

MySQL: Select non matching rows in a join

From Dev

Join two dataframes and overwrite matching rows [R]

From Dev

How to get matching and non matching rows from a SQL Join

From Dev

mysql join two table rows in one table

From Dev

MySQL join two table matching with their id

From Dev

Check for non matching rows in an outer join query with multiple keys

From Dev

two table join grid with distinct rows

From Dev

Is it possible to use the data.table index-join-assignment idiom to do a left join and assign NAs in the non-matching rows of i to x?

From Dev

MySQL Join; Query only non-matching table fields

From Dev

Join a table representing a "transfer" between two rows of another table

From Dev

Get matching rows between two tables and NULL otherwise on left table

From Dev

Delete rows in MySQL matching two columns in another table

From Dev

MySQL select row with two matching joined rows from another table

From Dev

Delete rows in MySQL matching two columns in another table

From Dev

Select partialy matching rows from two differernt table in KDB?

From Dev

How to select rows based on join on two columns of same table

From Dev

MySQL, Join two rows from one table and ignore doublons

From Dev

CodeIgniter two tables join with last table having optional rows

From Dev

Join two tables and show Columns of one table as Rows

From Dev

How to select rows based on join on two columns of same table

From Dev

how to join two table without producing useless rows

From Dev

Fetch single row from two rows of a table with join in SQL Server

From Dev

Join two files each with two columns, including non-matching lines

From Dev

Retrieve matching rows using join

From Dev

left join two tables on a non-unique column in right table

From Dev

roll join of two non-overlapping `data.table`s

Related Related

  1. 1

    LEFT JOIN table to find non matching rows, same table

  2. 2

    How to select non-matching rows from two mysql table

  3. 3

    How to join two table with non-existing rows

  4. 4

    MySQL: Select non matching rows in a join

  5. 5

    Join two dataframes and overwrite matching rows [R]

  6. 6

    How to get matching and non matching rows from a SQL Join

  7. 7

    mysql join two table rows in one table

  8. 8

    MySQL join two table matching with their id

  9. 9

    Check for non matching rows in an outer join query with multiple keys

  10. 10

    two table join grid with distinct rows

  11. 11

    Is it possible to use the data.table index-join-assignment idiom to do a left join and assign NAs in the non-matching rows of i to x?

  12. 12

    MySQL Join; Query only non-matching table fields

  13. 13

    Join a table representing a "transfer" between two rows of another table

  14. 14

    Get matching rows between two tables and NULL otherwise on left table

  15. 15

    Delete rows in MySQL matching two columns in another table

  16. 16

    MySQL select row with two matching joined rows from another table

  17. 17

    Delete rows in MySQL matching two columns in another table

  18. 18

    Select partialy matching rows from two differernt table in KDB?

  19. 19

    How to select rows based on join on two columns of same table

  20. 20

    MySQL, Join two rows from one table and ignore doublons

  21. 21

    CodeIgniter two tables join with last table having optional rows

  22. 22

    Join two tables and show Columns of one table as Rows

  23. 23

    How to select rows based on join on two columns of same table

  24. 24

    how to join two table without producing useless rows

  25. 25

    Fetch single row from two rows of a table with join in SQL Server

  26. 26

    Join two files each with two columns, including non-matching lines

  27. 27

    Retrieve matching rows using join

  28. 28

    left join two tables on a non-unique column in right table

  29. 29

    roll join of two non-overlapping `data.table`s

HotTag

Archive