开发者

SQL Help - Filter by User ID

开发者 https://www.devze.com 2023-02-03 04:47 出处:网络
SELECTprojectID, urlID, COUNT(1) AS totalClicks, projectPage, (SELECTCOUNT(1) FROMtblStatSessionRoutes, tblStatSessions
SELECT     projectID, urlID, COUNT(1) AS totalClicks, projectPage,
                          (SELECT     COUNT(1)
                            FROM          tblStatSessionRoutes, tblStatSessions
                            WHERE      tblStatSessionRoutes.statSessionID = tblStatSessions.ID AND tblStatSessions.projectID = tblAdClicks.projectID AND 
                                                   (tblStatSessionRoutes.leftPageID = tblAdClicks.projectPage OR
                                                   tblStatSessionRoutes.rightPageID = tblAdClicks.projectPage)) AS totalViews
FROM         tblAdClicks
GROUP BY projectID, urlID, projectPage
ORDER BY projectID, urlID

tblProjects has a field UserID, and I need this query to only show records where the project h开发者_如何学Goas userID = 5, so as to not show all the stats for all projects from all users


Naively:

SELECT     projectID, urlID, COUNT(1) AS totalClicks, projectPage,
                          (SELECT     COUNT(1)
                            FROM          tblStatSessionRoutes, tblStatSessions
                            WHERE      tblStatSessionRoutes.statSessionID = tblStatSessions.ID AND tblStatSessions.projectID = tblAdClicks.projectID AND 
                                                   (tblStatSessionRoutes.leftPageID = tblAdClicks.projectPage OR
                                                   tblStatSessionRoutes.rightPageID = tblAdClicks.projectPage)) AS totalViews
FROM         tblAdClicks
WHERE projectID IN (SELECT projectID FROM tblProjects WHERE userID = 5)
GROUP BY projectID, urlID, projectPage
ORDER BY projectID, urlID

But it might be better with a join. As always, check your execution plan


Well, I don't know the structure of the data that you have, but if nothing else, you can always join to the following subquery:

select projectID from tblProjects where userID=5

As to what column(s) you should use for the join, I can't really tell you, since I don't know the specifics of your data.

0

精彩评论

暂无评论...
验证码 换一张
取 消