如何根据查询条件过滤其他表的数据

Y
yff

具体需求是这样的,我有个仓库表,里面有仓库名称,仓库大小等信息,然后有张物品信息表,里面有个字段是仓库Id,需求方要求加个过滤条件,仓库有无空余空间。我要怎么实现?现在仓库使用多少,我是使用了虚拟字段,这个虚拟字段是所有sql查询出仓库占用的物品信息的。

回答14

超级管理员


可以使用这个试试,就是你自定义查询条件,也就是这个需要字段作为查询条件

yff
@超级管理员

你好,我看一下,没有这个条件

超级管理员
@yff

应该是你这个版本没有,最新版本是加上了的。或者你可以使用java增强来写列表功能

yff
@超级管理员

java怎么拼接sql?我想使用java,但是不知道怎么在前置增强中添加sql

可以试下sql 增强:

完整:

-- 仓库空位管理-仓库列表查询SQL -- 功能:查询所有未删除的仓库基础信息,并计算每个仓库的已使用空间和剩余可用空间 -- 支持通过only_vacant参数筛选:Y=仅显示有空余空间的仓库,N/空=显示所有仓库 SELECT tbl.id AS id, -- 仓库主键ID tbl.tenant_id AS tenant_id, -- 租户ID(多租户隔离字段) tbl.create_user AS create_user, -- 创建人ID tbl.create_time AS create_time, -- 创建时间 tbl.update_user AS update_user, -- 更新人ID tbl.update_time AS update_time, -- 更新时间 tbl.is_deleted AS is_deleted, -- 逻辑删除标记(0=未删除,1=已删除) tbl.create_dept AS create_dept, -- 创建部门ID tbl.warehouse_name AS warehouse_name, -- 仓库名称 tbl.warehouse_size AS warehouse_size, -- 仓库总容量(单位:根据业务定义,如立方米/平方等) -- 计算仓库已使用空间:关联子查询汇总该仓库下所有未删除的占用项占用空间之和 -- COALESCE函数处理空值:当没有占用项时返回0而不是NULL COALESCE(( SELECT SUM(i.occupy_space) FROM tbl_wh_vacancy_item i WHERE i.warehouse_id = tbl.id AND i.is_deleted = 0 ), 0) AS used_space, -- 计算仓库剩余可用空间:总容量 - 已使用空间 ( tbl.warehouse_size - COALESCE(( SELECT SUM(i.occupy_space) FROM tbl_wh_vacancy_item i WHERE i.warehouse_id = tbl.id AND i.is_deleted = 0 ), 0) ) AS free_space FROM tbl_wh_vacancy_warehouse tbl -- 仓库主表 WHERE tbl.is_deleted = 0 -- 只查询未被逻辑删除的仓库 -- 仓库空余状态筛选条件 AND ( -- 情况1:参数为空,不进行空余状态筛选 IFNULL(#{only_vacant}, '') = '' -- 情况2:参数为N,不进行空余状态筛选 OR #{only_vacant} = 'N' -- 情况3:参数为Y,仅筛选总容量大于已使用空间(即有剩余空间)的仓库 OR ( #{only_vacant} = 'Y' AND tbl.warehouse_size > COALESCE(( SELECT SUM(i.occupy_space) FROM tbl_wh_vacancy_item i WHERE i.warehouse_id = tbl.id AND i.is_deleted = 0 ), 0) ) ) -- JeecgBoot/Jeelowcode框架自动拼接的查询条件(由前端查询参数动态生成) AND #{jeelowcode_auto_where}

yff
@JeeLowCode技术支持-小海

这个地址是多少?好久没有登录过了

yff

我在sql增强中这样配置,查询报空指针异常,(select count(sn.id) as sn_quantity from tab_info sn where sn.is_deleted = 0 and sn.status = 1 and sn.pal_no = tbl.pal_no) as "snquantity" 这个是我设置的一个虚拟字段,虚拟字段使用sql查询,帮忙看看 这个哪里不对。。


select id as "id",

bat_no as "bat_no",

pal_no as "pal_no",

pstatus as "pstatus",

ptype as "ptype",

quantity as "quantity",

tenant_id as "tenant_id",

create_user as "create_user",

create_time as "create_time",

create_dept as "create_dept",

update_user as "update_user",

update_time as "update_time",

is_deleted as "is_deleted",

(select count(sn.id) as sn_quantity from tab_info sn where sn.is_deleted = 0 and sn.status = 1 and sn.pal_no = tbl.pal_no) as "snquantity"

from tab_palinfo tbl where 1=1

AND

IFNULL(#{sn_numstatus} '') = '',

OR #{sn_numstatus} = 0 and tbl.quantity > (select count(sn.id) as sn_quantity from tab_info sn where sn.is_deleted = 0 and sn.status = 1 and sn.pal_no = tbl.pal_no)

OR

#{sn_numstatus} = 1

AND tbl.quantity = (select count(sn.id) as sn_quantity from tab_info sn where sn.is_deleted = 0 and sn.status = 1 and sn.pal_no = tbl.pal_noo)

AND #{jeelowcode_auto_where}

yff
@JeeLowCode技术支持-小海

你好,有空帮忙看看上面那个sql对不对

yff
@超级管理员

我修改了一下sql,select id as "id",bat_no as "bat_no",pal_no as "pal_no",pstatus as "pstatus",ptype as "ptype",quantity as "quantity",tenant_id as "tenant_id",create_user as "create_user",create_time as "create_time",create_dept as "create_dept",update_user as "update_user",update_time as "update_time",is_deleted as "is_deleted",(select count(sn.id) as sn_quantity from tab_info sn where sn.is_deleted = 0 and sn.status = 1 and sn.pal_no = tbl.pal_no) as "snquantity" from tab_palinfo tbl where tbl.is_deleted = 0 AND #{jeelowcode_auto_where} 去掉哪些过滤,只保留了AND #{jeelowcode_auto_where},我不使用查询条件,可以查询出数据,只要增加查询条件,就抛空指针

java.lang.NullPointerException

 at com.jeelowcode.framework.plus.build.buildmodel.wrapper.SqlInfoQueryWrapper$Wrapper.buildSql(SqlInfoQueryWrapper.java:215)

 at com.jeelowcode.core.framework.config.aspect.enhance.JeeLowCodeAnnoaionAspectjSQL.getAutoWhereSql(JeeLowCodeAnnoaionAspectjSQL.java:559)

 at com.jeelowcode.core.framework.config.aspect.enhance.JeeLowCodeAnnoaionAspectjSQL.executeEnhanceAfterPage(JeeLowCodeAnnoaionAspectjSQL.java:318)

 at com.jeelowcode.core.framework.config.aspect.enhance.JeeLowCodeAnnoaionAspectjSQL.executSQLPlugin(JeeLowCodeAnnoaionAspectjSQL.java:215)

 at com.jeelowcode.core.framework.config.aspect.enhance.JeeLowCodeAnnoaionAspectjSQL.afterReturingAdvice(JeeLowCodeAnnoaionAspectjSQL.java:114)

 at sun.reflect.GeneratedMethodAccessor892.invoke(Unknown Source)

 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

 at java.lang.reflect.Method.invoke(Method.java:498)

超级管理员

看报错信息跟着调试一下,或者把最终运行的sql拿出来运行试试。

或者试一下 这样

where tbl.is_deleted = 0  #{jeelowcode_auto_where}

写回答

扫码关注

微信公众号二维码