2012-06-29 195 views
-1

我與此查詢,但沒有運氣嘗試:IN查詢選擇的所有記錄

SELECT tr.transferfrom, tr.transferto, br.id as 'BR_ID', 
    tr.refno, br.name as 'BR_NAME', br.code as 'BRANCH_CODE', 
    tr.docno, tr.transdate, stk.stockno,stk.salescat,tr.qty, 
    tr.sprice,tr.qty*tr.sprice as 'GROSS_SALES' 
FROM TRANSFER tr 
JOIN branch br on tr.branchid=br.id 
JOIN stocks stk on tr.stockid=stk.id 
WHERE (tr.docno = 'ST00576' AND br.id IN ('*')) 
OR RIGHT(tr.refno,7) = 'ST00576' AND LEFT(tr.refno,3) IN ('*') 

我想有查詢選擇的所有記錄,如果br.id是空值。

+0

在模式中,名爲'id'的列是否真的是空白的? –

回答

1

有一個子查詢得到所有的分支ID,然後在IN查詢可以參考子查詢的輸出..子查詢會像select br.id from branch br

因此,整個查詢將看起來像:

SELECT 
    tr.transferfrom, 
    tr.transferto, 
    br.id as 'BR_ID', 
    tr.refno, 
    br.name as 'BR_NAME', 
    br.code as 'BRANCH_CODE', 
    tr.docno, 
    tr.transdate, 
    stk.stockno, 
    stk.salescat, 
    tr.qty, 
    tr.sprice, 
    tr.qty*tr.sprice as 'GROSS_SALES' 
FROM TRANSFER tr 
    JOIN branch br 
    on tr.branchid=br.id 
    JOIN stocks stk 
    on tr.stockid=stk.id 
WHERE (
    tr.docno = 'ST00576' 
    AND br.id IN (select br.id from branch br) 
) 
    OR RIGHT(tr.refno,7) = 'ST00576' 
    AND LEFT(tr.refno,3) IN (select br.id from branch br) 
+0

忘了提及'*'是逗號分隔值,可能是空白..它是一個用戶輸入從複選框,我把它們連接成逗號分隔..但有時用戶可以將它留空以包括所有分支.. – redcoder