简体   繁体   中英

Unable to apply the filters on the Sql with Join

Below is my stored procedure. In which I am getting the details from multiple tables.

ALTER PROCEDURE [dbo].[proc_getCoachList] 
@skills varchar(MAX),  -------- example // '["Life", "Sports"]'
@keyword varchar(MAX)  -------- example // "developer"
AS   
BEGIN
(select SkillTitle from ValidSkills)
END

BEGIN
SELECT DISTINCT users.*, 
profiles.Details, 
profiles.Experiance, 
profiles.HoursCompleated,
profiles.RatePerHour, 
profiles.SubTitle,
profiles.TotalEarning,
UserSkils.UserSkills

FROM Users users 

LEFT JOIN profile profiles ON users.UserID = 
profiles.UserID

LEFT JOIN
(
    SELECT DISTINCT ts2.UserId, 
    (
        SELECT ts.SkillDescription + ',' AS [text()]
        FROM Skills ts
        WHERE ts.UserId = ts2.UserId 
        ORDER BY ts.UserId
        FOR XML PATH ('')
    ) AS UserSkills

    FROM Skills ts2

) UserSkils ON users.UserID = UserSkils.UserId

My filter starts from here---------

WHERE EXISTS (
     SELECT 1
                 FROM OPENJSON(@skills, '$') AS j
                 WHERE UserSkils.UserSkills LIKE '%' + j.value + '%'
) OR
users.UserName LIKE '%' + @keyword + '%'


END

Here My filters are not working properly. Filter is based on multiple skills and keyword.Please let me know the best way and proper filter inside the where clause..... The special case is when @skills are empty then its not working according keyword also.

The LIKE is a tricky function. The query table field ( j.value ) should be on the data side of the LIKE instruction (left), not on the match side (right). Maybe you can try using

WHERE CHARINDEX(j.value,UserSkils.UserSkills)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM