业务场景,根据查询参数List,动态拼接SQL语句。
例如:在工具中可执行的sql如下:
select a0.sku_id from
(select sku_id from wms_sku_attribute where attribute_list_id = '7')a0 inner join
(select sku_id from wms_sku_attribute where attribute_list_id = '11')a1 on a0.sku_id = a1.sku_id inner join
(select sku_id from wms_sku_attribute where attribute_list_id = '13')a2 on a0.sku_id = a2.sku_id
可能有a0 -- aN 个查询语句。
StringBuilder builder =new StringBuilder(); sql.append("select a0.sku_id from "); for(int i=0;i<list.size();i++){ builder.append("(select sku_id from wms_sku_attribute where attribute_list_id = '"+list.get(i)+"')a"+i); if(i==0){ sql.append(" inner join"); }else { sql.append(" on a0.sku_id = a"+i+".sku_id inner join"); } } String sql= builder.toString(); sql= sql.substring(0,sql.length()-("inner join").length());