我想使用select语句过滤表数据,我有四列,我还有四个文本框可以在每一列中启用搜索,当我在文本中输入值时,我可以在任何框中输入值box(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
答案 0 :(得分:2)
编辑:
SELECT
id
, firstname
, middlename
, lastname
FROM studentsinformation
WHERE id = @id
OR firstname LIKE '%' + @firstname + '%'
OR middlename LIKE '%' + @middlename + '%'
OR lastname LIKE '%' + @lastname + '%'
如果要为所有复选框选择true,则可以将OR换成AND。
答案 1 :(得分: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)
答案 2 :(得分: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 %"
这样做。
答案 3 :(得分: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%'