2017-10-13 76 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