2016-10-03 100 views
0

我有兩個表的用戶和收費比較兩個表中的數據,並獲取列表少見數據

我試圖讓所有的用戶列表,它不是來自前兩個月的計費表。

我新在SQL不知道如何能得到這個,我曾嘗試下面的SQL它不工作

select * subscribers UNION select *from charge; 

表結構: 用戶:

id || mobile|| subscribers_date 

負責人:

id || mobile || status || charge_date 

我的日期字段名稱是subscribers_date。

+0

分享你的表結構覆蓋索引與樣本輸入和期望輸出 – 1000111

回答

1

使用NOT IN運算符。

Select * from subscribers where 
    mobile not in (select mobile from charge where charge_date > '2 months ago') 

而且你charge表需要上charge_date, mobile

+0

它的工作對小型數據庫一起,我的表規模是巨大的,所以它是掛。 –

+0

你有索引subscribers_date,subscribers_id嗎? – Cine

+0

我試過了 select * from訂戶wheresubscribers.id不在(從d_date> = date_format(curdate(),'%​​Y-%m-01') - 間隔2個月) –