SELECT cpp.product_code AS productCode, cpp.product_name AS productName, cpp.risk_name AS riskName, cpp.risk_code AS riskCode, cpp.project, cpp.rate, cpp.tax_rate AS tax_rate, cpp.age, cpp.pay_period AS payPeriod, cpp.insurance_period AS insurancePeriod, cp.signatory_branch_name AS branchName, cp.signatory_branch_code AS branchCode, cp.protocol_code AS protocolCode, cp.protocol_name AS protocolName FROM coop_protocol_product cpp LEFT JOIN coop_protocol cp ON cpp.coop_protocol_id = cp.id <where> 1 = 1 <if test="dataList != null"> <foreach collection="dataList" index="index" item="list"> or ( <if test="list.riskCode != null and list.riskCode != ''"> cpp.risk_code = #{list.riskCode}</if> <if test="list.productCode != null and list.productCode != ''"> and cpp.product_code = #{list.productCode}</if> <if test="list.payPeriod != null and list.payPeriod != ''"> and cpp.pay_period = #{list.payPeriod}</if> <if test="list.insurancePeriod != null and list.insurancePeriod != ''"> and cpp.insurance_period = #{list.insurancePeriod}</if> <if test="list.age != null and list.age != ''"> and cpp.age = #{list.age}</if> ) </foreach> </if> </where> 这条sql会出错吗
时间: 2023-05-31 07:07:58 浏览: 100
list.riskCode </if> <if test="list.productCode != null and list.productCode != ''">and cpp.product_code = list.productCode </if> ) </foreach> </if> </where>
这个是用于查询保险产品信息的SQL语句,其中包含了产品编码、产品名称、风险名称、风险编码、项目、费率、税率、年龄、缴费期限、保险期限、签约分支机构名称、签约分支机构编码、协议编码、协议名称等信息。通过左连接查询coop_protocol_product表和coop_protocol表,其中coop_protocol_product是保险产品表,coop_protocol是协议表。where语句中的条件是根据参数dataList来添加的,如果dataList不为空,就会循环遍历其中的元素,根据riskCode和productCode是否为空来判断是否需要添加查询条件。
相关问题
function getPagedList(contractId) { var contractId = body.param.ontractId; SELECT a.id, a.conract_id contractId, a.product_id productId, a.purchase_num purchaseNum, a.purchase_price purchasePrice, a.remark, b.product_name productName, b.spec_code specCode, c.type_name typeName, d.unit_code unitCode FROM mes214820519.wms_pur_list_214820519 a, mes214820519.wms_product_214820519 b, mes214820519.wms_product_class_214820519 c, mes214820519.wms_unit_code_214820519 d WHERE a.product_id=b.id AND b.product_class_id = c.id AND b.unit_code_id=d.id `; return db.pager(sql, {contractId: contractId}) } 这段代码的错误在哪
这段代码有两个错误:
1. 在第一行中声明了 `contractId`,但在 SQL 查询语句中使用了 `body.param.ontractId`,应该改为 `body.param.contractId`。
2. SQL 查询语句的末尾多了一个反引号,应该删除它。
正确的代码应该是:
```
function getPagedList(contractId) {
var contractId = body.param.contractId;
var sql = `
SELECT
a.id,
a.conract_id contractId,
a.product_id productId,
a.purchase_num purchaseNum,
a.purchase_price purchasePrice,
a.remark,
b.product_name productName,
b.spec_code specCode,
c.type_name typeName,
d.unit_code unitCode
FROM
mes214820519.wms_pur_list_214820519 a,
mes214820519.wms_product_214820519 b,
mes214820519.wms_product_class_214820519 c,
mes214820519.wms_unit_code_214820519 d
WHERE
a.product_id=b.id
AND b.product_class_id = c.id
AND b.unit_code_id=d.id`;
return db.pager(sql, {contractId: contractId})
}
```
function getPagedList(contractId) { var contractId = body.param.contractId; var sql = ` SELECT a.id, a.contract_id contractId, a.product_id productId, a.purchase_num purchaseNum, a.purchase_price purchasePrice, a.remark, b.product_name productName, b.spec_code specCode, c.type_name typeName, d.unitcode unitCode FROM mes214820519.wms_pur_list_214820519 a, mes214820519.wms_product_214820519 b, mes214820519.wms_product_class_214820519 c, mes214820519.wms_unit_code_214820519 d WHERE a.product_id=b.id AND b.product_class_id = c.id AND b.unit_code_id=d.id AND a.contract_id=#{contractId}`; return db.pager(sql, {contractId: contractId}) } 用JavaScript语言修改这段代码
function getPagedList(contractId) {
var sql = `SELECT
a.id,
a.contract_id as contractId,
a.product_id as productId,
a.purchase_num as purchaseNum,
a.purchase_price as purchasePrice,
a.remark,
b.product_name as productName,
b.spec_code as specCode,
c.type_name as typeName,
d.unitcode as unitCode
FROM
mes214820519.wms_pur_list_214820519 a
INNER JOIN mes214820519.wms_product_214820519 b
ON a.product_id = b.id
INNER JOIN mes214820519.wms_product_class_214820519 c
ON b.product_class_id = c.id
INNER JOIN mes214820519.wms_unit_code_214820519 d
ON b.unit_code_id = d.id
WHERE
a.contract_id = :contractId`;
return db.pager(sql, { contractId: contractId });
}
在修改后的代码中,我使用了ES6模板字符串来构建SQL语句,同时使用了INNER JOIN来连接多张表,使代码更加简洁易读。另外,我还使用了命名参数代替了原来的占位符,以提高代码的可读性和可维护性。
阅读全文