2015-11-19 103 views
1

我有如下2個表:檢索所有沒有登錄用戶從MySQL表(SQL查詢)

+-------------+-----------+--------------+-------------------------------+ 
|        v3_customer        | 
+-------------+-----------+--------------+-------------------------------+ 
| customer_id | firstname | lastname |    email    | 
+-------------+-----------+--------------+-------------------------------+ 
|   1 | Piet  | Pizza  | [email protected]  | 
|   2 | Klaas  | Hein   | [email protected]  | 
|   3 | Henk  | Crowdcontrol | [email protected] | 
+-------------+-----------+--------------+-------------------------------+ 

+-------------+-------------+---------------+ 
|   v3_customer_activity   | 
+-------------+-------------+---------------+ 
| activity_id | customer_id |  key  | 
+-------------+-------------+---------------+ 
|   1 |   1 | login   | 
|   2 |   1 | order_account | 
|   3 |   2 | login   | 
+-------------+-------------+---------------+ 

我要的是選擇已經沒有登錄的所有客戶(注意登錄v3_customer_activity)。所以在這種情況下,這將是客戶customer_id:3

我正在使用mysql數據庫。

我曾嘗試使用下面的查詢嘗試:

SELECT DISTINCT v3_customer.customer_id, v3_customer.firstname, v3_customer.lastname, v3_customer.email FROM `v3_customer` JOIN v3_customer_activity ON v3_customer.customer_id = v3_customer_activity.customer_id WHERE v3_customer.customer_id != (SELECT v3_customer_activity.customer_id FROM v3_customer_activity) 

在此希望它會在子查詢中發現的行之間循環。 這導致了一個錯誤,告訴我一個子查詢可能不包含多行。

TL; DR

我想要的是檢索v3_customer每一個客戶誰不是在表v3_customer_activity

回答

3

上市試試這個:

select v3_customer.* from v3_customer 
left join v3_customer_activity on v3_customer.customer_id=v3_customer_activity.customer_id 
where v3_customer_activity.customer_id is null; 

LEFT JOIN v3_customerv3_customer_activity和不匹配的過濾器記錄。

+0

非常感謝:D! – Baklap4

+0

我不知道v3_customer。*是有效的mysql,你知道的越多! – Baklap4

+0

@ Baklap4:沒有憂慮。我的榮幸 :) –

0
select v3_customer.* from v3_customer 
where v3_customer.customer_id not in (SELECT v3_customer_activity.customer_id FROM v3_customer_activity) 
相關問題