2017-10-13 47 views
0

我有2個表MySQL的逆選擇的結果我想找到的不匹配的結果:從2個表

table1 

    REF CARD AMOUNT 
    12345 55432 1000 
    23456 55321 2000 
    34567 55789 3000 


    table2 

    MSG    AMT ID 
    12345_T1R3-55432 1000 456 
    23456-R1M5-55321 2000 567 
    34567*[email protected] 5000 783 

我的查詢:

SELECT 
    'table1'.'REFF', 
    'table2'.'MSG', 
    'table1'.'card', 
    'table1'.`amount`, 
    'table2'.'amt' 
FROM 
    'table1', 
    'table2' 
WHERE 
    'table2'.'MSG' LIKE CONCAT('%', 'table1'.'REFF', '%') AND 'table2'.'MSG' LIKE CONCAT('%', 'table1'.'card', '%') 

查詢結果

table1.reff table2.msg   table1.card table1.amount table2.amt 
12345   12345_T1R3-55432 55432   1000   1000 
23456   23456-R1M5-55321 55321   2000   2000 

我想從這個結果得到其他結果(反相),如:

table1.reff table2.msg   table1.card table1.amount table2.amt 
34567   34567*[email protected] 55789   3000   5000 

謝謝

+1

您對單引號的使用完全不正確。 –

+0

我正在使用MySQL Front,並且該應用程序自動給出了引號 – Andy

+0

請參閱https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-我要非常簡單的sql查詢 – Strawberry

回答

0

P And Q相反的是not P or not Q。因此,將此應用於您的查詢產生:

SELECT 
    t1.REFF, 
    t1.MSG, 
    t1.card, 
    t1.amount, 
    t2.amt, 
FROM table1 t1 
LEFT JOIN table2 t2 
    ON t2.MSG LIKE CONCAT('%', t1.REFF, '%') AND 
     t2.MSG LIKE CONCAT('%', t1.card, '%') 
WHERE 
    t2.MSG IS NULL; 

順便說一句,您使用單引號是完全不正確的。您發佈的查詢可能不會運行,所以我猜測可能是您錯誤地複製了它。

+0

謝謝,但我從這個查詢得到的結果是與LIKE查詢之前添加NOT相同 – Andy

+0

@Andy請嘗試更新後的查詢。 –

+0

查詢時間過長,所以我重新啓動了MySQL服務,並獲得了當前結果,但t2.MSG值爲NULL。我可以讓MSG字段不顯示NULL嗎?謝謝 – Andy