次のクエリでは、次のような結果が得られました。しかし、LIKE 条件は、これらの多くの場合には当てはまりません。SQl Fiddle
簡単に言うと、numMatches 列に「Missing」キーワードの一致が 5 つある理由です。
パラメーター に検索語として missing を渡しました@SearchItems
。
ALTER PROCEDURE ntwolose.SPSearchFilter (@SearchItems varchar,
@StartPos int,
@EndPos int)
AS
SELECT
*
FROM (SELECT
numMatches,
ProductCode,
ProductName,
ProductType,
Brand,
Color,
[Size],
Model,
GetPlace,
LostPlace,
DatePosted,
TimePosted,
Username,
Updatedate,
Status,
Title,
ROW_NUMBER() OVER (ORDER BY numMatches DESC) AS RatingRank
FROM (SELECT
ProductCode,
ProductName,
ProductType,
Brand,
Color,
[Size],
Model,
GetPlace,
LostPlace,
DatePosted,
TimePosted,
Username,
Updatedate,
Status,
Title,
(CASE
WHEN ProductCode LIKE '%' + @SearchItems + '%' THEN 1
ELSE 0
END +
CASE
WHEN ProductName LIKE '%' + @SearchItems + '%' THEN 1
ELSE 0
END +
CASE
WHEN Brand LIKE '%' + @SearchItems + '%' THEN 1
ELSE 0
END +
CASE
WHEN Color LIKE '%' + @SearchItems + '%' THEN 1
ELSE 0
END +
CASE
WHEN [Size] LIKE '%' + @SearchItems + '%' THEN 1
ELSE 0
END +
CASE
WHEN Model LIKE '%' + @SearchItems + '%' THEN 1
ELSE 0
END +
CASE
WHEN GetPlace LIKE '%' + @SearchItems + '%' THEN 1
ELSE 0
END +
CASE
WHEN LostPlace LIKE '%' + @SearchItems + '%' THEN 1
ELSE 0
END +
CASE
WHEN DatePosted LIKE '%' + @SearchItems + '%' THEN 1
ELSE 0
END +
CASE
WHEN Username LIKE '%' + @SearchItems + '%' THEN 1
ELSE 0
END +
CASE
WHEN Title LIKE '%' + @SearchItems + '%' THEN 1
ELSE 0
END +
CASE
WHEN ProductType LIKE '%' + @SearchItems + '%' THEN 1
ELSE 0
END
)
AS numMatches
FROM TBLProductDetails)
AS RatedResultTable)
AS PagedResultSet
WHERE RatingRank BETWEEN @StartPos AND @EndPos
numMatches ProductCode ProductName ProductType Brand Color Size Model GetPlace LostPlace DatePosted TimePosted Username Updatedate Status Title RatingRank
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------------ -------------------------------------------------- -------------------------------------------------- ------------------------------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5 cv,cf,cf cgm cvh cvhhcv cvmgcvh cvmgcmhc cmv hcvh 2wes dddd cmhcvmh 2012-12-12 00:00:00.0000000 12.11 a2a 2013-05-20 17:27:49.6370000 1 sree 1
4 P90 C510 Mobile SOny Black 12X12 M90 Ernakulam 2013-12-02 00:00:00.0000000 12.opm a@a 2013-05-22 19:12:05.0330000 1 Sony Ercison Missing 2
3 P 9090 Black Leather Leather Bata Black 34 inches M90 Malapuram Kerala 2013-02-03 00:00:00.0000000 3:4 pm sreekalac1990 2013-05-27 17:11:37.5570000 1 Got Mirror Bags 3
3 P12 Documents Mobile Noikiya red 123 678 Calicut Cochin 2013-12-02 00:00:00.0000000 2:9:0 sree@gmail.comm 2013-05-17 10:17:13.8670000 1 nair 4
3 P15 Documents Mobile Noikiya red 123 678 Cochin Calicut 2013-12-02 00:00:00.0000000 2:9:0 sree@gmail.comm 2013-05-17 10:17:13.0000000 1 bigshen 5
2 P123 Lg Torch LG white 12 MD90 Ernakulam 2013-12-02 00:00:00.0000000 12.00pm a@a 2013-05-20 09:51:50.9930000 1 c 6
2 P9099 fgvf fdfv fbf ffbh 34 fhbfgh fvgbngfvn hgnjhgmj 2013-05-05 00:00:00.0000000 3 sreekalac1990 2013-05-27 17:18:04.7100000 1 Subvibn Missing 7
2 123 mobile ret4546 samsung ertet dgfdg 12 1234 ewtret345 2012-12-12 00:00:00.0000000 12:89 123243 2013-05-20 17:05:44.2170000 1 Title 8
1 fbgnh12 7266 mobile nokia Silver 72 x 66 12343 dddd Pallikkal 2013-12-04 00:00:00.0000000 12.00 Am a2a 2013-05-20 17:19:50.1970000 1 kala 9
0 fghf fghfh fhfgh fghgfh fghfh fghfgh fhfgh fhgfgh fghfgh 2013-12-04 00:00:00.0000000 fghfg a@a 2013-05-28 20:29:09.6700000 1 dhgfgh 10
0 aaaaaaaaaaaaa jsdofjs dhfgkjdsgkjkj djsfdsjf sdfjlskd 5465 dfsdf filed hjdsfjfjks 2012-12-12 00:00:00.0000000 11.10 Pm a2a 2013-05-20 17:25:03.2730000 1 title 11
0 sdil k 89 jkkb uihui 889 xcv jbkbk jkbk 2013-03-03 00:00:00.0000000 2 a@a 2013-05-29 09:57:48.3330000 1 Shoe 12