如何根据多条件在数据库里进行查询?急!!!

caoit 2003-08-30 04:39:43
我想根据栏目类别,栏目名,日期等进行查询.现在的情况是当我有一项不选项时就不根据这个条件查询
...全文
69 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnhgj 2003-08-30
  • 打赏
  • 举报
回复
mysql = "select * from chanpingzhanshi_table where "
if textbox1.text<>"" then
mysql=mysql & " and title='"+textbox1.text+"'"
end if
if dropdownlist1.selectedindex<>0 then
mysql=mysql & " and xangyedalei='"+dropdownlist1.selecteditem.text+"'"
end if
if dropdownlist2.selectedindex<>0 then
mysql=mysql & " and xangyezilei='"+dropdownlist2.selecteditem.text+"'"
end if
if dropdownlist3.selectedindex<>0 then
mysql=mysql & " and changdi='"+dropdownlist3.selecteditem.text+"'"
end if
mysql = mysql & " order by cpid desc"
mysql = replace(mysql,"where and","where")
mysql = replace(mysql,"where order","order")
Response.Write(mysql)
hanfey 2003-08-30
  • 打赏
  • 举报
回复
following the above: chenxy2002((HomeMan) and luckysusan(海闊天空) ( )
it will be okay!
klbt 2003-08-30
  • 打赏
  • 举报
回复
动态生成条件语句比较好,可以重用。
luckysusan 2003-08-30
  • 打赏
  • 举报
回复
根据栏目类别,栏目名,日期在代码里面动态生成 where 后的查询条件!没什么难的!
假设:
字段名: type name tdate
web控件名: ddl_type txt_name txt_tdate

dim QueryStr as string="select * from table "

if ddl_type.selecteditem.value <> "" then

QueryStr = QueryStr & " where type='" & ddl_type.selecteditem.value & "' "
if txt_name.text.trim <> "" then QueryStr = QueryStr & " and txt_name='" & txt_name.text & "' "
if txt_tdate.text.trim <> "" then QueryStr = QueryStr & " and txt_tdate='" & txt_tdate.text & "' "

elseif txt_name.text.trim <> "" then

QueryStr = QueryStr & " where txt_name='" & txt_name.text & "' "
if txt_tdate.text.trim <> "" then QueryStr = QueryStr & " and txt_tdate='" & txt_tdate.text & "' "

else

if txt_tdate.text.trim <> "" then QueryStr = QueryStr & " where txt_tdate='" & txt_tdate.text & "' "

end if

TomLi98011 2003-08-30
  • 打赏
  • 举报
回复
运用sp_executesql
例子如下:
@NewsTitle nvarchar(1000)=null,
@NewsKeyWord nvarchar(100)=null,
@NewsContent nvarchar(1000)=null,
@NewsTypeId int=0,
@NewsProvince int=0,
@NewsProductTypeID int=0,
@NewsProductID int=0,
@MainNews int=0,
@NewsState int=9,
@NewsDate datetime=null
AS
DECLARE @strSQL nvarchar(1000)
DECLARE @strChoice nvarchar(1000)
DECLARE @strNewsTitle nvarchar(1000)
DECLARE @strNewsKeyWord nvarchar(1000)
DECLARE @strNewsContent nvarchar(1000)

BEGIN
SET @strChoice = 'NewsState = @ckNewsState '
SET @strNewsTitle = '%'+@NewsTitle+'%'
SET @strNewsKeyWord = '%'+@NewsKeyWord+'%'
SET @strNewsContent = '%'+@NewsContent+'%'

If @NewsTitle <> ''
BEGIN
SET @strChoice = @strChoice + ' AND NewsTitle LIKE @ckNewsTitle'
END

If @NewsKeyWord <> ''
BEGIN
SET @strChoice = @strChoice + ' AND NewsKeyWord LIKE @ckNewsKeyWord'
END

If @NewsContent <> ''
BEGIN
SET @strChoice = @strChoice + ' AND NewsContent LIKE @ckNewsContent'
END

If @NewsTypeId <> 0
BEGIN
SET @strChoice = @strChoice + ' AND NewsTypeId = @ckNewsTypeId'
END

If @NewsProvince <> 0
BEGIN
SET @strChoice = @strChoice + ' AND NewsProvince = @ckNewsProvince'
END

If @NewsProductTypeID <> 0
BEGIN
SET @strChoice = @strChoice + ' AND NewsProductTypeID = @ckNewsProductTypeID'
END

If @NewsProductID <> 0
BEGIN
SET @strChoice = @strChoice + ' AND NewsProductID = @ckNewsProductID'
END

If @MainNews <> 2
BEGIN
SET @strChoice = @strChoice + ' AND MainNews = @ckMainNews'
END

SET @strSQL = 'SELECT NewsId, NewsTitle, NewsFileName, NewsDate '+
'FROM Web_News WHERE '+@strChoice+' ORDER BY NewsDate DESC'
EXECUTE sp_executesql @strSQL,
N'@ckNewsState int, @ckNewsTitle nvarchar(1000), @ckNewsKeyWord nvarchar(100), @ckNewsContent nvarchar(1000),
@ckNewsTypeId int=0, @ckNewsProvince int, @ckNewsProductTypeID int, @ckNewsProductID int, @ckMainNews int',
@ckNewsState=@NewsState, @ckNewsTitle=@strNewsTitle, @ckNewsKeyWord=@strNewsKeyWord, @ckNewsContent=@strNewsContent,
@ckNewsTypeId=@NewsTypeId, @ckNewsProvince=@NewsProvince, @ckNewsProductTypeID=@NewsProductTypeID, @ckNewsProductID=@NewsProductID, @ckMainNews=@MainNews
END
GO
xinyu_1980 2003-08-30
  • 打赏
  • 举报
回复
用Like就可以了.
"select * from YourTable where 字段1 like '%" & 所选值.trim & "%'"

如果 所选值.trim="" 时即查询所有数据
jvhmr 2003-08-30
  • 打赏
  • 举报
回复

偶是苯方法:通过在每个DropDownList中添加"-"项,
当DropDownList1.SelectedItem.Value="-"时重写查询语句
cqluojia 2003-08-30
  • 打赏
  • 举报
回复
你可以根据要查询的内容来动态设置DataAdapter的SelectCommand不就搞定了。
siugwan 2003-08-30
  • 打赏
  • 举报
回复
动态生成SQL语句就得了;先检查查询的内容,如果有空白的就跳开,把有查询的内容生成SQL语句再赋到DataAdapter
chenxy2002 2003-08-30
  • 打赏
  • 举报
回复
WHERE后加一个永远成立的条件。例如1=1

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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