MySQL count of count,将一个 table 的结果与另一个一起使用

MySQL count of count, use result from one table with another

我是来问这个问题的实现的 MYSQL count of count?

我的问题是将我从一个 table 中提取结果的结果联系起来,使用它们查询同一数据库的另一个 table (抱歉,我对 xySQL 不在行).

我有一个名为 ticketsdb 的 MySQL 数据库,我需要根据 table2 (post 上的计数从 table1 (tickettb) 中提取数据stb)

场景: 更准确地说,碰巧票中的 posts 有回复,我需要离开 poststb table,所有票只计算 2 posts . 从该结果中,我需要从 tickettb table 中提取所有相应的票证。 正如您可能猜到的那样,poststb 仅包含票据标识、票证日期 post、主题和消息,所有其他用户信息都在 tickettb 中。

到目前为止,我成功地(感谢上面的post)从poststb的两个回复中拉出了posts,但是因为他们进入了游戏别名,我卡住了。

代码是这样

SELECT PostsOfTheGivenTicket, TicketIdentification 
FROM (SELECT posttb.ticket_id, COUNT(posttb.ticket_id) AS 
             PostsOfTheGivenTicket, posttb.ticket_id as TicketIdentification 
      FROM posttb GROUP BY posttb.ticket_id 
      HAVING PostsOfTheGivenTicket= 2) AS TemporaryTable

(也许这不是我的目标的最佳选择,所以感谢您的任何确认)

总之,测试了一下,returns 全部 83 张工单只有 2 条消息

以上查询结果如下

 PostsOfTheGivenTicket    TicketIdentification 
 2                        1234
 2                        1451
 2                        1526

看起来它被分配给 temporay/aliased table 临时表(是吗?如何重新使用它?)

在 tickettb 中 table 我有一列名称已设置为 IdTicket,它存储相同的值

IdTicket  UserName UserPhone UserEmail etc etc
1234
....
1451
....
1526

我的目标是提取(最终在网络服务器上)与匹配票相关的所有票据详细信息,换句话说

SELECT from tickettb every ticket with IdTicket equal to TicketIdentification

是否可以将上述查询嵌套到另一个实现目标的查询中?

虽然最终目标,将转向select并删除部分带有2条消息的门票,例如只有超过 10 天的。

SELECT * FROM ticketb WHERE IdTicket IN
(
      SELECT ticket_id
      FROM posttb 
      GROUP BY ticket_id 
      HAVING COUNT(ticket_id) = 2
)