2013-10-07 30 views
2

如何獲取table_1不在表2中的ID
我的表看起來像這種類型。
TABLE_1
如何獲取不在表2中的值

--------------------------------------------- 
| id  | user      | 
--------------------------------------------- 
| 1  | Jack      | 
--------------------------------------------- 
| 2  | John      | 
--------------------------------------------- 

TABLE_2

------------------------------------------ 
| web_id  | website    | 
------------------------------------------ 
| 2   | Facebook    | 
------------------------------------------ 
| 3   | Google+    | 
------------------------------------------ 

我想查詢

$this->db->select("*"); 
     $this->db->from("table_1"); 
     $this->db->where_not_in('id', "table_2.web_id"); 
     return $this->db->get(); 
+0

如何在這些2個表有關? – DevelopmentIsMyPassion

+0

我認爲ID和web_id – zzlalani

+0

我已經添加了兩種方式下面的查詢 –

回答

0

像這樣的事情會做(取決於什麼˚F你使用SQL的lavour你可能要稍微調整語法):

SELECT id 
FROM table_1 
WHERE id NOT IN (SELECT web_id FROM table_2) 

不過,我看不出有什麼用,這是。這兩個表之間沒有可識別的鏈接,因此,我們知道table_1中有1個id,但table_2中沒有web_id,1個似乎不是有用的信息。

+0

是它的核心查詢,但是當我嘗試它codeigniter這個查詢可以告訴我什麼錯誤在這個查詢'$ this-> db-> select(「 *「); \t \t $ this-> db-> from($ this - > _ table); \t \t $ this-> db-> where_not_in('id',$ this - > _ tuple。「。tuple_regexs_id」); \t \t return $ this-> db-> get();' – user2846831

+0

@ user2846831我不用PHP編寫代碼,所以我不能告訴你這是什麼問題。如果您遇到的是PHP代碼的問題,那麼您需要在問題中明確說明問題(並確保包含代碼的相關部分!),因爲現在它讀作「什麼是SQL這個?」 –

2

試試這個

SELECT id 
FROM table_1 
WHERE id NOT IN 
(
     SELECT web_id FROM table_2 WHERE 1 
); 

用笨的活動記錄查詢將如下

$this->db->select('*'); 
    $this->db->where('id NOT IN (SELECT web_id FROM table_2 WHERE 1)', NULL, FALSE); 
    $query = $this->db->get('table_1'); 
0
select t1.id 
from table_1 t1 
left join table_2 t2 on t1.id = t2.web_id 
where t2.web_id is null 
相關問題