2012-10-17 50 views
0

首先,我很抱歉我的英語。左連接與和md5(concat())比較mysql

我想兩個記錄,生成MD5比較:

我插入表2中,我從表1

INSERT INTO table2 (id_table_2, hash_string) 
SELECT t.id, MD5 (CONCAT (t.firstname, t.lastname)) AS hash_string 
FROM table1 t WHERE t.id = $some_value 

帶來從那以後,我想知道是什麼記錄從表1有一個在不存在信息table2,但我無法獲得我想要的結果。我這樣做:

SELECT t.id, MD5(CONCAT(t.firstname, t.lastname)) , ti.hash_string 
FROM table1 t 
    LEFT JOIN table2 ti ON (t.id = ti.id_table_2 
    AND MD5(CONCAT(t.firstname, t.lastname)) != ti.hash_string) 
    WHERE t.state = 2 

但它沒有工作。

我想要的是表1中不在表2中的記錄,但是從那裏,如果有md5散列不同也表示它。但我沒有得到它。我感謝你能給我的所有幫助。謝謝。

回答

1

也許你想這樣,看到最後添加的行,我也改變!==

SELECT t.id, MD5(CONCAT(t.firstname, t.lastname)) , ti.hash_string 
FROM  table1 t 
LEFT JOIN table2 ti ON (t.id = ti.id_table_2 
        AND MD5(CONCAT(t.firstname, t.lastname)) = ti.hash_string) 
WHERE t.state = 2 
    AND ti.id_table_2 IS NULL 

這一招的基本形式:

SELECT * 
FROM  a 
LEFT JOIN b ON a.id = b.id 
WHERE b.id IS NULL 
+0

優秀的biziclop,這就是爲我工作。非常感謝。這就是我需要的。 –

+0

很高興,如果它幫助:) – biziclop

0

我相信這應該幫助你(移動hash_string加入並追加爲where子句):

SELECT t.id, MD5(CONCAT(t.firstname, t.lastname)) , ti.hash_string 
FROM table1 t 
LEFT JOIN table2 ti ON (t.id = ti.id_table_2) 
WHERE t.state = 2 
    AND MD5(CONCAT(t.firstname, t.lastname)) != ti.hash_string 
    AND ti.id_table_2 is NULL; 
+0

yogendra謝謝,我也試過,但沒有工作。他給我看了所有的記錄。感謝 –

+0

通過'MD5(CONCAT(t.firstname,t.lastname))!= ti.hash_string'你應該得到記錄HashString不匹配的地方。答案中還增加了一個'Null condition'。請檢查。 –

+0

優秀,它也適用於我。非常感謝Yongendra。 @Yogendra –