通过like关键字进行搜索的SQL查询 [英] Sql query for search by like keyword
问题描述
我正在为我的asp.net网站创建搜索功能。我有一个文本框和按钮,用户可以在文本框中输入任何搜索词,我通过存储过程检查它是否存在于我的表中。 @cat参数包含用户搜索词...
I am creating search fuctionality for my asp.net website. I have one textbox and button, user can enter any search term in textbox and i checks whether it is present in my table or not through stored procedure. @cat parameter contains user search term...
ALTER PROCEDURE [dbo].[sps_search]
@cat nvarchar(100)
AS
BEGIN
select * from tbl_adregister where category like '%'+@cat+'%'
END
在表(tbl_adregister)中,列(类别)存储类别ID,并且存在用于存储类别名称(category_name)和id的单独表(tbl_category) (category_id)。
我的问题是用户输入tex搜索框中的t值以及如何将其与表格匹配(tbl_category)。
In table (tbl_adregister) the column(category) stores the category id and there is seperate table(tbl_category) for storing category names(category_name) and id(category_id).
My problem is user enters text value in search box and how can i match it with the table(tbl_category).
推荐答案
使用SQL JOIN [ ^ ]
use SQL JOIN[^]
select ta.* from tbl_adregister ta join tbl_category tc on ta.category_id =tc.category_id where tc.category_name like '%'+@cat+'%'
Belwo语句应该完成工作;)
Belwo statement should do the work ;)
select r.*, c.*
from tbl_adregister AS r INNER JOIN tbl_category AS c ON r.category = c.category_id
where c.category like %@cat%
这篇关于通过like关键字进行搜索的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!