SQL查询帮助,按客户端ID过滤

时间:2011-02-23 14:59:06

标签: sql

SELECT
    tblArtworkTemplates.ID,
    tblArtworkTemplates.userID as creatorID,
    tblArtworkTemplates.dateCreated,
    tblSpecifications.TxtPagination, 
    tblSpecifications.FlatSizeW AS width,
    tblSpecifications.FlatSizeL AS length,
    tblSpecifications.FlatSizeUOM AS uom,
    (SELECT COUNT(1) AS Expr1
        FROM tblArtworkUploads
        WHERE (templateID = tblArtworkTemplates.ID)) AS uploadCount,
    (SELECT COUNT(1) AS talks
        FROM tblArtworkTemplateMessages
        WHERE (templateID = tblArtworkTemplates.ID)) AS talkCount,
    tblUsers.id AS editUserID,
    tblUsers.userName,
    CAST((CASE WHEN DATEDIFF(n, tblArtworkTemplates.lastEditPing, getDate()) < 5 THEN 1 ELSE 0 END) AS bit) AS 'IsInLast5Mins'
FROM
    tblUsers RIGHT OUTER JOIN
    tblArtworkTemplates INNER JOIN
    tblSpecifications
        ON tblArtworkTemplates.specID = tblSpecifications.id
        ON tblUsers.id = tblArtworkTemplates.editPingUserID
WHERE
    (tblArtworkTemplates.userID = @userID)

这适用于传递用户ID以进行过滤。但是,tblUsers中的每个用户都有一个clientID。我希望不是按用户ID过滤,而是按客户端ID过滤。

所以我传入客户端ID = 21的位置,并返回创建它的用户的客户端ID为21的所有记录的列表。

我知道这是一个令人难以置信的无聊问题,而且头脑麻木,但任何帮助都非常受欢迎。

编辑:表格结构

tblArtworkTemplates:
  - ID
  - userID (who created it)

tblUsers
  - ID
  - clientID

因此,我想过滤客户端ID,而不是过滤容易因为存储在tblArtworkTemplates中的userID。因此,如果我传入客户端ID 21,它将获取所有图形模板记录,其中userID具有该客户端ID。

1 个答案:

答案 0 :(得分:1)

SELECT
    tblArtworkTemplates.ID,
    tblArtworkTemplates.userID as creatorID,
    tblArtworkTemplates.dateCreated,
    tblSpecifications.TxtPagination, 
    tblSpecifications.FlatSizeW AS width,
    tblSpecifications.FlatSizeL AS length,
    tblSpecifications.FlatSizeUOM AS uom,
    (SELECT COUNT(1) AS Expr1
        FROM tblArtworkUploads
        WHERE (templateID = tblArtworkTemplates.ID)) AS uploadCount,
    (SELECT COUNT(1) AS talks
        FROM tblArtworkTemplateMessages
        WHERE (templateID = tblArtworkTemplates.ID)) AS talkCount,
    tblUsers.id AS editUserID,
    tblUsers.userName,
    CAST((CASE WHEN DATEDIFF(n, tblArtworkTemplates.lastEditPing, getDate()) < 5 THEN 1 ELSE 0 END) AS bit) AS 'IsInLast5Mins'
FROM
    tblUsers RIGHT OUTER JOIN
    tblArtworkTemplates INNER JOIN
    tblSpecifications
        ON tblArtworkTemplates.specID = tblSpecifications.id
        ON tblUsers.id = tblArtworkTemplates.editPingUserID
WHERE
    (tblArtworkTemplates.creatorID IN (Select ID From tblUsers 
     Where clientID = @clientID)

您的查询似乎tblArtworkTemplates.editPingUserIDtblUsers.id