我有以下帶有 SQL 查詢的代碼PreparedSentence: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如果我通過:進(jìn)行搜索,效果會很好WHERE P.CODE = ? AND C.SITEGUID = ?。P.REFERENCE但是,假設(shè)我希望它在if 中沒有匹配的情況下找到結(jié)果P.CODE。我嘗試執(zhí)行這樣的代碼語句,但沒有成功:WHERE P.CODE OR P.REFERENCE = ? AND C.SITEGUID = ?,但收到錯誤。任何幫助將不勝感激。
2 回答

揚帆大魚
TA貢獻(xiàn)1799條經(jīng)驗 獲得超9個贊
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);

繁星coding
TA貢獻(xiàn)1797條經(jīng)驗 獲得超4個贊
你的語法錯誤,應(yīng)該是
WHERE (P.CODE = ? OR P.REFERENCE = ?) AND C.SITEGUID = ?
然后你需要設(shè)置第三個參數(shù)
添加回答
舉報
0/150
提交
取消