-1

I am trying to use NOT IN statement with MySQL. However, I get 0 row with code below (no syntax error). I am sure there should be more than 0 row with the statement. What syntax should I adjust?

SELECT DISTINCT member_id
FROM client_payments
INNER JOIN client_purchase_records ON client_purchase_records.id = client_payments.purchase_record_id
WHERE status = 1
AND client_payments.created_at > '2021-10-28 00:00:00'
AND client_payments.created_at < '2021-10-31 23:59:00'
NOT IN(
    SELECT DISTINCT member_id
    FROM client_payments
    INNER JOIN client_purchase_records ON client_purchase_records.id = client_payments.purchase_record_id
    WHERE status = 1
    AND client_payments.created_at > '2020-9-30 00:00:00'
    AND client_payments.created_at < '2021-10-27 23:59:00'
);

Difference about two query is mainly about created_at column, I want to do "set difference operation" with period A(2021-10-28 00:00:00 - 2021-10-31 23:59:00 )and period B(2020-9-30 00:00:00 - 2021-10-27 23:59:00)

  1. I want to query out member_id who pay during 2020-9-30 00:00:00 - 2021-10-27 23:59:00

  2. Subtract with member_id who pay during 2021-10-28 00:00:00 - 2021-10-31 23:59:00

  3. Finally I get member_id who pay during 2021-10-28 00:00:00 - 2021-10-31 but not pay during 2020-9-30 00:00:00 - 2021-10-27 23:59:00 ( new member_id never show before)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
fung
  • 641
  • 6
  • 14
  • Unless I'm missing something, your query is: "Find members like X that are not like X". What are you trying to get? – Álvaro González Nov 02 '21 at 08:36
  • Please share more details. What do you want to achieve? What have you tried to resolve the problem? – Nico Haase Nov 02 '21 at 08:44
  • 1
    `AND x < y NOT IN (SELECT whatever)` is wrong but **syntactically correct**. You are not getting syntax errors, yes, but the query is wrong. – Salman A Nov 02 '21 at 08:46
  • Difference about two query is mainly about `created_at` column, I want to do "set difference operation" with period A(2021-10-28 00:00:00 - 2021-10-31 23:59:00 )and period B(2020-9-30 00:00:00 - 2021-10-27 23:59:00) – fung Nov 02 '21 at 08:49
  • 1. I want to query out member_id who pay during 2020-9-30 00:00:00 - 2021-10-27 23:59:00 2. Subtract with member_id who pay during 2021-10-28 00:00:00 - 2021-10-31 23:59:00 3. Finally I get member_id who pay during 2021-10-28 00:00:00 - 2021-10-31 but not pay during 2020-9-30 00:00:00 - 2021-10-27 23:59:00 ( new member_id never show before) – fung Nov 02 '21 at 08:51

2 Answers2

0

You presumably want to be saying AND member_id NOT IN (...your subquery...); as is, it is using the previous condition instead of member_id.

Also, it looks like you want >= and <=, not > and <.

I would do this like so instead:

SELECT member_id
FROM client_payments
INNER JOIN client_purchase_records ON client_purchase_records.id = client_payments.purchase_record_id
WHERE status = 1
AND client_payments.created_at >= '2021-09-30 00:00:00'
AND client_payments.created_at <= '2021-10-31 23:59:00'
GROUP BY member_id
HAVING MIN(client_payments.created_at) >= '2021-10-28 00:00:00'
ysth
  • 96,171
  • 6
  • 121
  • 214
0

no syntax error

The error is in the logic.

Your condition, after adding the parenthesis according to operators priority, looks like

AND ( (client_payments.created_at < '2021-10-31 23:59:00') NOT IN ( {subquery} ) )

I.e. the result of comparing client_payments.created_at < '2021-10-31 23:59:00' (which is 0, 1 or NULL) is searching in the subquery output which is obviously illogical.

Akina
  • 39,301
  • 5
  • 14
  • 25