谁能帮我解决这个sql查询

我有以下带有 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如果我通过:进行搜索,效果会很好WHERE P.CODE = ? AND C.SITEGUID = ?。P.REFERENCE但是,假设我希望它在if 中没有匹配的情况下找到结果P.CODE。我尝试执行这样的代码语句,但没有成功:WHERE P.CODE OR P.REFERENCE = ? AND C.SITEGUID = ?,但收到错误。任何帮助将不胜感激。


翻阅古今
浏览 107回答 2
2回答

扬帆大鱼

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

你的语法错误,应该是WHERE (P.CODE = ?  OR P.REFERENCE = ?) AND C.SITEGUID = ?然后你需要设置第三个参数
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java