0

我有一个 HTML 表,它是从查找系统上所有用户的“用户”表的查询中生成的。

第二个查询使用“选择计数”来计算用户预订了多少次会议,方法是检查用户 ID 在会议表中出现的次数。

<cfquery datasource="iPad" name="Two">
 SELECT COUNT(userID) AS meetingsCount from meeting where userID = '#One.userID#'
 </cfquery>

我希望能够通过单击页面上的链接根据 meetingCount 重新排序表格。问题是我不确定如何查询这些信息,因为它在技术上不存在于 MYSQL 的表中。

编辑; 使用左连接和验证的代码。

select user.userID, user.contactName, user.email, count(meeting.userID)
as meetingsCount 
 from user where user.userID = 30 
AND user.userID NOT IN ('1', '2', '3', '4', '58', '59', '62',
'63', '64', '66', '69', '71',    '72', '73', '78', '107')
AND SUBSTRING( meeting.meetingCode, 5, 2 ) 
BETWEEN 12 AND 22 
AND SUBSTRING( meeting.meetingCode, 7, 2 ) 
BETWEEN 1 AND 12 
AND SUBSTRING( meeting.meetingCode, 9, 2 ) 
BETWEEN 01 AND 31
left outer join meeting on user.userID = meeting.userID
4

1 回答 1

6

您应该使用 group by 一个查询来执行此操作

编辑:我将您的 where 子句添加到我的答案中,但不是硬编码用户 ID 以排除您,您应该考虑将它们分配给一个角色,以便您可以在一个地方管理角色,而不是排除这些人的每个查询。not in如果您已经要求一个 ID,您也不需要。我知道你现在只是在解决问题,但请记住这一点。如果那仍然不起作用,我会检查您的子字符串逻辑。

<cfquery>
    select
          user.userID
        , firstName
        , lastName
        , count(*) as meetingCount
    from
        user
        left outer join meeting on users.userID = meeting.userID
          AND (    SUBSTRING( meeting.meetingCode, 5, 2 ) BETWEEN 12 AND 22 
               AND SUBSTRING( meeting.meetingCode, 7, 2 ) BETWEEN 1 AND 12 
               AND SUBSTRING( meeting.meetingCode, 9, 2 ) BETWEEN 1 AND 31)
      -- the ( ) above are optional but I like them to show groups of like filters
      -- if you don't want to use them feel free not to.
    where 
         user.userID NOT IN (1,2,3,4,5,59,62,63,64,66,69,71,72,73,78,107)     
    group by
          user.userID
        , firstName
        , lastName
    order by
        meetingCount
</cfquery>

我认为 MySql 允许您按别名排序,我不记得了。如果没有,将顺序更改为count(*)

于 2013-02-15T12:17:39.367 回答