这样的,我一个数据表里分品名,编号,规格等几个。其中编号唯一,我想根据品名得出编号,这个可以的,但有这种情况,那就是同一个品名,规格不同,这个也可以得出。最困难的是同样的品名,有一个是空值,请问怎么办?我用select * from table where pinming='"+ combobox1.text +"' and guige='"+combobox2.text+"'。
不知我说清楚没,比如品名为“斧子”的共有两个,其中一个标注了规格为“铁制”,而另一个是空值。那么我想根据品名和规格的条件得到编号。如果斧子+铁制,那么就是00001,如果是斧子+“”,那么就是00002.我试过好多次,只能得出00001,没办法得出00002.请高手帮我。
不知我说清楚没,比如品名为“斧子”的共有两个,其中一个标注了规格为“铁制”,而另一个是空值。那么我想根据品名和规格的条件得到编号。如果斧子+铁制,那么就是00001,如果是斧子+“”,那么就是00002.我试过好多次,只能得出00001,没办法得出00002.请高手帮我。
select * from table where pinming='"+ combobox1.text +"' and (guige='"+combobox2.text+"' or isnull(guige,'')='')
1楼的语句没用,结果会依然.
declare @table table (pinming varchar(4),guige varchar(4))
insert into @table
select '斧子','铁制' union all
select '斧子',nullselect * from @tabledeclare @guige varchar(6)set @guige=null --结果出空值那个
--set @guige='铁制' --结果出铁制那个
select * from @table where pinming='斧子' and isnull(guige,'null')=isnull(@guige,'null')
如果它为空就不要加条件到sql语句里了,combobox1也一样。
有些在程序代码里用好过点吧?
一楼,我试了,提示:用于函数参数的个数不对,在查询表达式pinming='电缆'and (guige='3米'orisnull(guige,'')='')中,错在哪儿了,麻烦再给看看。
二楼,你说的我没看懂,因为一般的查询为空就是全部,我只想根据这个空也能查出来对应的ID。
四楼,太麻烦了,但也谢谢你。
有两笔记录,
ID pinming guige
001 斧子 铁制
002 斧子
我根据select * from table where pinming='"+ combobox1.text +"' and guige='"+combobox2.text+"'查询,如果条件是斧子和铁制,那么可以得到ID号为001
但如果条件是斧子和空(这也是一个下拉框选择)就得到两笔,也就是全部了,单独得不到002.
我想以斧子和空查出来。
呵呵,啰嗦了。
select * from where 字段 is null--查询为空的
if(combobox2.text!="")//判断提交是否是空值
string sql_str="select * from table where pinming='"+ combobox1.text +"' and guige='"+combobox2.text+"'";
else
string sql_str="select * from table where pinming='"+ combobox1.text +"' and guige is null";
思路清了,就是is null可以查出来,=‘’不可以,特别感谢guguda2008。
但如果用select * from table where pinming='"+ combobox1.text +"' and (guige='"+combobox2.text+"' or isnull(guige,'')='')
怎么改呢,想简单点。