怎么给已有的SQL语句用程序添加查询条件

怎么给已有的SQL语句用程序添加查询条件,第1张

用流程控制语句拼接就可以了,比如(Java为例):

String sql="select * from XXX where 1=1"

if(条件)sql+=" and 字段=值"

if(条件)sql+=" and 字段=值"

if(条件)sql+=" and 字段=值"对于编程语言来说,sql就是一个字符串,拼接成能执行的sql就行了。

提供一个简单思路:

1:找出李四的权限

select qx from a where xm='李四'

2:找出张三的权限

select qx from a where xm='张三'

3:使用子查询找出李四有的权限,张三没有的权限

select qx from a where xm='张三' and qx not in (select qx from a where xm='李四')

4:生成数据

insert into a(xm,qx,tf) select ,'张三',qx,1 from a where xm='张三' and qx not in (select qx from a where xm='李四')


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/7906949.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-11
下一篇2023-04-11

发表评论

登录后才能评论

评论列表(0条)

    保存