SQL - 如何过滤特定数据而不是返回所有结果?

时间:2015-12-24 19:44:38

标签: sql

在这个查询中,我几乎得到了我需要的一切。唯一的问题是我想要消除DON&T满足两个标准的应用程序。

标准:

  

显示app_id,其中field_id的rel_data为22222 ='' AND rel_data for field_id 55555 = U。

SELECT distinct app_id, alr.loan_id, field_id, rel_data from loan_field_rel lfr
LEFT JOIN app_loan_rel alr ON alr.loan_id=lfr.loan_id
WHERE field_id IN (22222, 55555) AND rel_data IN ('', 'U') 
GROUP BY app_id, rel_data, alr.loan_id, field_id
ORDER BY app_id

我应该只看到2808条结果,但我得到13,000个结果:

app_id          loan_id    field_id     rel_data
25791           791        22222    
25791           791        55555        U

我知道这很简单,但感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

... where ( field_id = 22222 and rel_data = '' ) OR ( field_id = 55555 and rel_data = 'U' )

校正:

SELECT distinct app_id, alr.loan_id, field_id, rel_data 
from app_loan_rel alr
LEFT JOIN loan_field_rel lfr22222 ON alr.loan_id=lfr.loan_id and field_id = 22222
LEFT JOIN loan_field_rel lfr55555 ON alr.loan_id=lfr.loan_id and field_id = 55555
WHERE lfr22222.rel_data = ''
    AND lfr55555.rel_data = 'U'
GROUP BY app_id, rel_data, alr.loan_id, field_id
ORDER BY app_id

答案 1 :(得分:0)

  where  (  field_id=2222 and rel_data='') or (field_id=5555 and rel_data=U)