3

我想使用 select 语句过滤表数据,我有四列,还有四个文本框可以在每列中启用搜索,当我在文本框中输入值时,我可以在任何框中输入值(es ) 我想返回与我输入的值匹配的记录,我该怎么做?

ALTER PROCEDURE dbo.test_search

    (
        @ID int,
    @FirstName nvarchar(50),
    @MiddleName nvarchar(50),
    @LastName nvarchar(50)
    )

AS
    SELECT     ID, FirstName, MiddleName, LastName
    FROM         StudentsInformation
    WHERE (@ID IS NULL OR StudentsInformation.ID = @ID) AND
           (@FirstName IS NULL OR StudentsInformation.FirstName = @FirstName )AND
           (@MiddleName IS NULL OR StudentsInformation.MiddleName = @MiddleName )AND
           (@LastName IS NULL OR StudentsInformation.LastName = @LastName )
    RETURN
4

4 回答 4

2

编辑:

SELECT 
id
, firstname
, middlename
, lastname    
FROM studentsinformation
WHERE id = @id        
OR firstname LIKE '%' + @firstname + '%'
    OR middlename LIKE '%' + @middlename + '%'
    OR lastname LIKE '%' + @lastname + '%'

如果要选择对所有复选框都为真的记录,可以将 OR 交换为 AND。

于 2013-04-24T07:16:02.363 回答
0

我希望这会奏效:

Select * from <table-name> 
where 
<Column-name1> Like 'TextBox1%'
or
<Column-name2> Like 'TextBox2%'
or
<Column-name3> Like 'TextBox2%'
or
<Column-name4> Like 'TextBox4%'
于 2013-04-24T07:39:53.623 回答
0

首先,您需要找到传递text-box了哪个搜索字符串。

根据文本框,查询可以写在相关column.

select * from table_name where column like '%text-box value%'

编辑

SELECT ID,FirstName,MiddleName,LastName
FROM  StudentsInformation
WHERE 1=1
ID=(case when @ID <>0 AND @ID IS NOT NULL then @ID else ID end)
and FirstName=(case when @FirstName<>'' and @FirstName IS NULL then @FirstName 
else FirstName)
and MiddleName=(case when @MiddleName<>'' and @MiddleName IS NULL then @MiddleName 
else MiddleName)
and LastName=(case when @LastName<>'' and @LastName IS NULL then @LastName 
else LastName)
于 2013-04-24T07:14:54.007 回答
0

语法取决于您使用的编程语言。

但一般来说:

string sql="select * from tableName where"

if((txt1.Text!="")&&(sql=="select * from tableName where")
sql=sql+"colName like % @txt1 %"
else
sql=sql+" and colName like % @txt1 %"



if((txt2.Text!="")&&(sql=="select * from tableName where")
sql=sql+"colName like % @txt2 %"
else
sql=sql+" and colName like % @txt2 %"


if((txt3.Text!="")&&(sql=="select * from tableName where")
sql=sql+"colName like % @txt3 %"
else
sql=sql+" and colName like % @txt3 %"

这样做。

于 2013-04-24T07:15:46.323 回答