sql(case when 条件)

liangling221 2008-05-26 01:09:11
$pro_select ="select a.products_id,a.sgg,a.sdw,a.products_price,a.products_erp,
c.products_id,c.products_name,d.idppbh,d.sppmc
from osc_products a,osc_products_description c,brand d
where a.products_id =c.products_id and a.products_id
in (select b.products_id from brand_area_product b where b.idppbh= d.idppbh
and b.idppbh = '" . $brand . "' //如果b.idppbh没有指定为具体的某个值则去掉and b.idppbh = '" . $brand . "' 这个查询条件,如果指定则加上这个条件)
and c.products_name like '%". $pro_name ."%' ";

如果b.idppbh没有指定为具体的某个值则去掉and b.idppbh = '" . $brand . "' 这个查询条件,如果指定则加上这个条件这个 sql语句怎样改?大家多多指教谢谢,,,

$pro_select ="select a.products_id,a.sgg,a.sdw,a.products_price,a.products_erp,
c.products_id,c.products_name,d.idppbh,d.sppmc
from osc_products a,osc_products_description c,brand d
where a.products_id =c.products_id and a.products_id
in (select b.products_id from brand_area_product b where b.idppbh= d.idppbh
case when 。。。。。。then ....
and b.idppbh = '" . $brand . "'
) and c.products_name like '%". $pro_name ."%' ";
...全文
3400 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzq3554055 2008-07-23
  • 打赏
  • 举报
回复
自身相等 。。。。
arrow_gx 2008-05-26
  • 打赏
  • 举报
回复
这好像和 case when 没什么关系吧
Limpire 2008-05-26
  • 打赏
  • 举报
回复
$pro_select ="select a.products_id,a.sgg,a.sdw,a.products_price,a.products_erp,
c.products_id,c.products_name,d.idppbh,d.sppmc
from osc_products a,osc_products_description c,brand d
where a.products_id =c.products_id and a.products_id
in (select b.products_id from brand_area_product b where b.idppbh= d.idppbh
and b.idppbh = isnull(nullif('" . $brand . "',''),b.idppbh) //如果b.idppbh没有指定为具体的某个值则去掉and b.idppbh = '" . $brand . "' 这个查询条件,如果指定则加上这个条件)
and c.products_name like '%". $pro_name ."%' ";
-狙击手- 2008-05-26
  • 打赏
  • 举报
回复
不明
hery2002 2008-05-26
  • 打赏
  • 举报
回复
楼上有点笔误 :(
declare @idppbh nvarchar(10)
set @idppbh ='1234'
select a.products_id,a.sgg,a.sdw,a.products_price,a.products_erp,
c.products_id,c.products_name,d.idppbh,d.sppmc
from osc_products a,osc_products_description c,brand d
where a.products_id =c.products_id and a.products_id
in (select b.products_id from brand_area_product b where b.idppbh= d.idppbh
and b.idppbh = isnull(@idppbh,b.idppbh) -- 这样
and c.products_name like '%". $pro_name ."%'
hery2002 2008-05-26
  • 打赏
  • 举报
回复
类似与这样,自己改改!:)
declare @idppbh nvarchar(10)
set @idppbh ='1234'
select a.products_id,a.sgg,a.sdw,a.products_price,a.products_erp,
c.products_id,c.products_name,d.idppbh,d.sppmc
from osc_products a,osc_products_description c,brand d
where a.products_id =c.products_id and a.products_id
in (select b.products_id from brand_area_product b where b.idppbh= d.idppbh
and b.idppbh = isnull(@idppbh '. $brand . '
and c.products_name like '%". $pro_name ."%'
arrow_gx 2008-05-26
  • 打赏
  • 举报
回复
//如果b.idppbh没有指定为具体的某个值则去掉and b.idppbh = '" . $brand . "' 这个查询条件,如果指定则加上这个条件 ??

楼主的意思好像是要组成动态语句, case when 的条件,没有给出来呢,不好分析,提醒一下LZ ,case when 最后面要跟一个end

case when ......then .... end

hery2002 2008-05-26
  • 打赏
  • 举报
回复
使用 isnull(col,'')这种方式就可以了

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧