简体   繁体   English

无法通过联接在SQL上应用过滤器

[英]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. 过滤器基于多种技能和关键字。请让我知道where子句中的最佳方法和适当的过滤器.....特殊情况是@skills为空时,它也不根据关键字起作用。

The LIKE is a tricky function. LIKE是一项棘手的功能。 The query table field ( j.value ) should be on the data side of the LIKE instruction (left), not on the match side (right). 查询表字段( j.value )应该在LIKE指令的数据侧(左),而不是在匹配侧(右)。 Maybe you can try using 也许您可以尝试使用

WHERE CHARINDEX(j.value,UserSkils.UserSkills)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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