私は単純なテーブルUSERSとORDERSを2つ持っており、何も注文していないUSERSを探したいと思っています。注文ごとに私はuserId
の記入欄を持っています。私は、単純なクエリを記述するが、彼女は作品...LEFT JOIN文は、NOT INを含むサブクエリと似ていませんか?
単純なクエリはありません:
select id from USERS where id not in (select userId from ORDERS);
はUSERS.id
がORDERS.userId
ようVARCHAR
であることに注意してください。
誰かがそれを説明することができます:
> select count(distinct userId) from ORDERS;
+------------------------+
| count(distinct userId) |
+------------------------+
| 4261 |
+------------------------+
1 row in set (0.04 sec)
> select count(distinct id) from USERS;
+--------------------+
| count(distinct id) |
+--------------------+
| 14960 |
+--------------------+
1 row in set (0.00 sec)
> select id from USERS
> where id not in (select userId from ORDERS);
Empty set (0.47 sec)
は私が注文のユーザーよりも多くのユーザーを持っていますが、SETは空です!これは、14960から4261 =
でなければなりません。しかし、私はそれを行うとき:
> select count(*)
> from USERS left join ORDERS on ORDERS.userId=USERS.id
> where ifnull(ORDERS.userId,'')='';
+----------+
| count(*) |
+----------+
| 10700 |
+----------+
1 row in set (0.14 sec)
それはOKです! 1行にエラーがあります(0または空値のためnullになる可能性があります...)
多分私は疲れていますが、それ以降は...私はMySQLには何も分かりません。 !!