2014-03-03 97 views
1

選擇行我有一個名爲表「來電」, 欄目有:加入表與自己和SQL

a_imei_number (imei of caller) 
b_imei_number (imei of number which is called) 
a_phone_number (number of caller) 
b_phone_number (number of number which is called) 
call_start_time (as hour) 
call_end_time (as hour) 
call_day (as day) 

我試圖找到whic已經在相交的時候有克隆的IMEI號,以便調用必須至少有2行,所以phone_numbers必須不同,並且imei_numbers相同。像

id a_imei b_imei a_phone b_phone call_start_time call_end_time call_day 
1 A  b  123  321  13:30   13:50   01.02.2014 
2 A  c  999  888  13:41   13:55   01.02.2014 

我該如何加入自己的表?

注意:一行的a_imei可能與其他行的b_imei相同,因爲可能有一個克隆的手機正在呼叫並且該手機的克隆被某人調用。

+0

加入斜面你只是這樣做......'調用a,調用b WHERE a.column = b.column'? – TheOneWhoPrograms

+0

不,我不能說這就是爲什麼我問這個問題 – abidinberkay

+1

你能向我們展示一些來自表格的樣本數據,以及你希望選擇從樣本數據中選擇什麼以及不選擇什麼?乍一看,我不知道爲什麼你不能按照我上面的建議去做。 – TheOneWhoPrograms

回答

0

以下是如何讓所有的a_imei克隆:

SELECT c1.* 
FROM calls As c1 
JOIN calls As c2 
    ON c1.a_imei   = c2.a_imei 
    AND c1.a_phone   != c2.a_phone 
    AND c1.call_start_time < c2.call_end_time 
    AND c1.call_end_time > c2.call_start_time 
ORDER BY c1.a_imei, c1.call_start_time, c1.call_end_time 

下面是如何比較這兩個 「一」 S和 「b」 S在一起:

SELECT c1.*, c2.* 
FROM calls As c1 
JOIN calls As c2 
    ON c1.call_start_time < c2.call_end_time 
    AND c1.call_end_time > c2.call_start_time 
    AND ( (c1.a_imei  = c2.a_imei AND c1.a_phone != c2.a_phone) 
     OR (c1.a_imei  = c2.b_imei AND c1.a_phone != c2.b_phone) 
     OR (c1.b_imei  = c2.a_imei AND c1.b_phone != c2.a_phone) 
     OR (c1.b_imei  = c2.b_imei AND c1.b_phone != c2.b_phone) 
     ) 
ORDER BY c1.a_imei, c1.call_start_time, c1.call_end_time, c1.b_imei 
+0

a和b必須在同一個查詢中,因爲a_imei和b_imei也可以相交,例如你和我使用相同的imei(克隆非法)你叫別人,所以你的imei在a_imei和相同有人在b_imei專欄中給我打電話和我的imei--我們將使用其電話號碼相同且同時具有不同電話號碼的電話。 – abidinberkay

+0

我會提出一個小建議,即第一個電話小於第二個電話,所以它不會做相反的操作,並返回phoneA + phoneB對和phoneB + phoneA對的相同行集合... – DRapp