●●急●求一簡單的sql語句●●●在線解決馬上給分
現有3個條件實現他們任意的組合的查詢語句
1.String GuanLiNo = request.getParameter( "GuanLiNo ");
2.String Projectname = request.getParameter( "Projectname ");
3.String PartNo = request.getParameter( "PartNo ");
sql= "select * from PartPress where (GuanLiNo = ' "+GuanLiNo+ " ' and PartNo= ' "+PartNo+ " ') or (GuanLiNo = ' "+GuanLiNo+ " ' and Projectname = ' "+Projectname+ " ') or (GuanLiNo = ' "+GuanLiNo+ " ' and Projectname = ' "+Projectname+ " ' and PartNo= ' "+PartNo+ " ') " ;
我寫的sql怎麼不對。
請大家幫忙寫寫
[解决办法]
select *
from PartPress
where (@GuanLiNo is null or GuanLiNo=@GuanLiNo)
and (@Projectname is null or Projectname=@Projectname)
and (@PartNo is null or PartNo=@PartNo)
[解决办法]
直接使用 OR 就好了。。。
sql= "select * from PartPress where (GuanLiNo = ' "+GuanLiNo+ " ' or Projectname = ' "+Projectname+ " ' or PartNo= ' "+PartNo+ " ') "
[解决办法]
这个最好放在程序前台进行处理。
同意wangtiecheng的观点.
[解决办法]
--纯SQL Server
declare @GuanLiNo varchar(100),@Projectname varchar(100),@PartNo varchar(100)
select *
from PartPress
where (@GuanLiNo is null or GuanLiNo=@GuanLiNo)
and (@Projectname is null or Projectname=@Projectname)
and (@PartNo is null or PartNo=@PartNo)
[解决办法]
这是纯SQL Server环境,指的是在查询分析器中执行的。
不是在你的前台代码中写的。
前台写,需要用if 分开写。
[解决办法]
--前台代码处理方式,如下:
--具体前台你的开发工具如何判断是否为空,自己改一下
sql= "select * from PartPress where 1=1 "
if GuanLiNo 不为空
sql=sql + " and GuanLiNo = ' "+GuanLiNo+ " ' "
if Projectname 不为空
sql=sql + " and Projectname = ' "+Projectname+ " '
if PartNo 不为空
sql=sql+ " and PartNo= ' "+PartNo+ " ' ";