我在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 = ?
使用搜索,效果很好。但是,可以说,如果P.REFERENCE
中没有匹配项,我希望它在P.CODE
中找到结果。我尝试执行这样的代码语句,但未成功:WHERE P.CODE OR P.REFERENCE = ? AND C.SITEGUID = ?
,但收到错误。任何帮助将不胜感激。
答案 0 :(得分:1)
将您的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);
答案 1 :(得分:0)
您的语法错误,应该是
WHERE (P.CODE = ? OR P.REFERENCE = ?) AND C.SITEGUID = ?
然后您需要设置第三个参数