0

我正在尝试编写 sql 查询来显示用户已添加书签的搜索抛出资源。我的问题是当用户键入关键字时,搜索将搜索网站上的所有资源,包括他的书签我希望能够搜索用户书签

SELECT
    a.Id ,
    a.summary ,
    a.pageId ,
    a.name ,
    a.createdOn ,
    a.userID ,
    Total
FROM Resources AS a
LEFT JOIN Topics_Resources AS b ON a.Id = b.ResourceID
LEFT JOIN Skills_Resources AS c ON a.Id = c.ResourceID
INNER JOIN Modules AS m ON a.ModuleId = m.ModuleID
INNER JOIN ContentItems AS ci ON m.ModuleID = ci.ModuleID
INNER JOIN Tabs AS t ON t.TabID = ci.TabID
INNER JOIN TabModules AS tb ON t.TabID = tb.TabID
INNER JOIN Bookmarks ON a.Id = Bookmarks.resourceID
INNER JOIN Bookmarks AS Bookmarks_1 ON a.Id = Bookmarks_1.resourceID
INNER JOIN
(
    SELECT
        r.Id , COUNT(l.resourceId) Total
    FROM resources r
    LEFT JOIN likes l ON r.Id = l.resourceid
    GROUP BY r.Id
)
l ON a.Id = l.Id
LEFT OUTER JOIN HtmlText AS h ON tb.ModuleID = h.ModuleID
WHERE (h.content LIKE '%winter%')
OR (t.description LIKE '%winter%')
OR (t.keywords LIKE '%winter%')
OR (a.summary LIKE '%winter%')
AND Bookmarks.userID = '695A8626-A28B-4308-AD9B-D5AC576B4E5A'
GROUP BY    a.Id ,
            a.summary ,
            a.pageId ,
            a.name ,
            a.createdOn ,
            a.userID ,
            Total
ORDER BY a.createdOn DESC
4

1 回答 1

1

尝试将每个 OR 括在括号中

SELECT  a.Id, a.summary, a.pageId, a.name, a.createdOn,a.userID,Total
FROM         Resources AS a 
LEFT JOIN Topics_Resources AS b ON a.Id = b.ResourceID 
LEFT JOIN Skills_Resources AS c ON a.Id = c.ResourceID 
INNER JOIN Modules AS m ON a.ModuleId = m.ModuleID 
INNER JOIN ContentItems AS ci ON m.ModuleID = ci.ModuleID 
INNER JOIN Tabs AS t ON t.TabID = ci.TabID 
INNER JOIN TabModules AS tb ON t.TabID = tb.TabID 
INNER JOIN Bookmarks ON a.Id = Bookmarks.resourceID 
INNER JOIN Bookmarks AS Bookmarks_1 ON a.Id = Bookmarks_1.resourceID 
INNER JOIN (select r.Id, count(l.resourceId) Total
            from resources r
            left join likes l on r.Id = l.resourceid
            group by r.Id)  l 
    on a.Id  = l.Id 
LEFT OUTER JOIN HtmlText AS h ON tb.ModuleID = h.ModuleID 
where ((h.content like '%winter%') or ( t.description like '%winter%') or ( t.keywords like '%winter%') or ( a.summary like '%winter%'))
    and Bookmarks.userID='695A8626-A28B-4308-AD9B-D5AC576B4E5A'
GROUP BY  a.Id, a.summary, a.pageId, a.name, a.createdOn,a.userID,Total  
ORDER BY a.createdOn DESC

注意:如果您预计数据量很大,这种查询将持续数小时。你应该添加全文

于 2013-07-04T21:10:46.247 回答