簡體   English   中英

誰能幫我解決這個 sql 查詢

[英]Can anyone help me with this sql query

我在PreparedSentence中有以下代碼和 SQL 查詢:

public final ProductInfoExt getProductInfoByCode(String sCode, String siteGuid) throws BasicException {
    if (sCode.startsWith("977")) {
        // This is an ISSN barcode (news and magazines) 
        // the first 3 digits correspond to the 977 prefix assigned to serial publications, 
        // the next 7 digits correspond to the ISSN of the publication 
        // Anything after that is publisher dependant - we strip everything after  
        // the 10th character 
        sCode = sCode.substring(0, 10);
    }
    return (ProductInfoExt) new PreparedSentence(s, "SELECT "
            + getSelectFieldList()
            + " FROM STOCKCURRENT AS C RIGHT JOIN PRODUCTS P ON (C.PRODUCT = P.ID) "
            + " WHERE P.CODE OR (P.REFERENCE = ? ) AND C.SITEGUID = ? ",
            new SerializerWriteBasicExt(new Datas[]{Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING}, new int[]{0, 1}),
            ProductInfoExt.getSerializerRead()).find(sCode, siteGuid);
}

如果我通過P.CODE使用搜索,它會很好用: WHERE P.CODE =? AND C.SITEGUID =? WHERE P.CODE =? AND C.SITEGUID =? . 但是,可以說如果P.CODE中沒有匹配項,我希望它在P.REFERENCE中找到結果。 我嘗試執行這樣的代碼語句但沒有成功: WHERE P.CODE OR P.REFERENCE =? AND C.SITEGUID =? WHERE P.CODE OR P.REFERENCE =? AND C.SITEGUID =? ,但我收到一個錯誤。 任何幫助將不勝感激。

將您的OR語句分組

return (ProductInfoExt) new PreparedSentence(s, "SELECT "
        + getSelectFieldList()
        + " FROM STOCKCURRENT AS C RIGHT JOIN PRODUCTS P ON (C.PRODUCT = P.ID) "
        + " WHERE (P.CODE = ? OR P.REFERENCE = ?) AND C.SITEGUID = ? ",
        new SerializerWriteBasicExt(new Datas[]{Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING}, new int[]{0, 1, 2}),
        ProductInfoExt.getSerializerRead()).find(sCode, sCode, siteGuid);

你的語法錯誤,應該是

WHERE (P.CODE = ?  OR P.REFERENCE = ?) AND C.SITEGUID = ?

然后你需要設置第三個參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM