2013-06-05 77 views
0
I have a query: 
SELECT 
ZIPSECvc255Zip,ZIPSECCODIServiceType 
FROM towSearch_ZIP_ZipCodesSecondary 
WHERE ZIPSECCOMlAPAssignee = 57055 
AND ZIPSECCODlAPStatus = 302 
    AND ZIPSECCODIServiceType in (263,1012) 
    AND ZIPSECADDIKey = 314105 

I am getting follow result: 
ZIPSECvc255Zip ZIPSECCODIServiceType 
    17001    263 
    17003    1012 
    17007    1012 
    17003    263 

所有值滿足我想是因爲它與這兩個ZIPSECCODIServiceType(即263,1012)相關的17003只單個記錄的記錄。我怎樣才能實現使用查詢?檢索從MySQL,對於在IN子句

回答

0

這應該做

SELECT ZIPSECvc255Zip FROM 
(SELECT ZIPSECvc255Zip, count(*) AS cnt 
    FROM towSearch_ZIP_ZipCodesSecondary 
    WHERE ZIPSECCOMlAPAssignee = 57055 
    AND ZIPSECCODlAPStatus = 302 
    AND ZIPSECCODIServiceType in (263,1012) 
    AND ZIPSECADDIKey = 314105 
    GROUP BY ZIPSECvc255Zip) 
WHERE CNT = 2 

您將要替換2號與你想要的corrispondences,他們應該改變(即你有多少個號碼中有你的狀態)。

0
SELECT ZIPSECvc255Zip,ZIPSECCODIServiceType 
FROM towSearch_ZIP_ZipCodesSecondary 
WHERE ZIPSECCOMlAPAssignee = 57055 
AND ZIPSECCODlAPStatus = 302 
AND ZIPSECCODIServiceType = 263 and 
ZIPSECvc255Zip in 
    (
    select ZIPSECvc255Zip FROM towSearch_ZIP_ZipCodesSecondary 
     WHERE ZIPSECCOMlAPAssignee = 57055 
     AND ZIPSECCODlAPStatus = 302 
     AND ZIPSECCODIServiceType = 1012 
     AND ZIPSECADDIKey = 314105 
    ) 
AND ZIPSECADDIKey = 314105 
0

我希望這會有所幫助。

SELECT 
ZIPSECvc255Zip,ZIPSECCODIServiceType 
FROM towSearch_ZIP_ZipCodesSecondary 
WHERE rowid = (SELECT max(rowid) FROM towSearch_ZIP_ZipCodesSecondary WHERE ZIPSECCOMlAPAssignee = 57055 AND ZIPSECCODlAPStatus = 302 
AND ZIPSECCODIServiceType in (263,1012) 
AND ZIPSECADDIKey = 314105 GROUP BY count(ZIPSECvc255Zip)>1) 
0
SELECT DISTINCT 
t1.ZIPSECvc255Zip,t1.ZIPSECCODIServiceType 
FROM towSearch_ZIP_ZipCodesSecondary as t1 INNER JOIN towSearch_ZIP_ZipCodesSecondary as t2 
ON (t1.ZIPSECvc255Zip = t2.ZIPSECvc255Zip AND t1.ZIPSECCODIServiceType = '263' AND t2.ZIPSECCODIServiceType = '1012') 
WHERE 
t1.ZIPSECCOMlAPAssignee = 57055 
AND 
t1.ZIPSECCODlAPStatus = 302 
AND 
t1.ZIPSECADDIKey = 314105 

http://sqlfiddle.com/#!2/325ba/9