Hakutaku 2019-09-20 01:19 采纳率: 100%
浏览 225
已结题

MYSQL 根据条件写一个SQL statement

图片说明

要求:For each user taking a step, calculate how many other users have taken the same step. We are only interested in the cases where the step is performed by at least 5 other users. Display the user ID, number of other users (at least 5 other users) who are taking the same step and the title of the taken step.

以下我写的:
1: 先找出所有taking step 的other user 大于 5 的
图片说明

2。再找userID

SELECT distinct u.userID,count(u.userID),s.title
FROM user u
INNER JOIN step_taken st ON (st.user_id = u.userID)
INNER JOIN step s ON (st.step_id = s.stepID)
GROUP BY u.userID, s.title
HAVING s.title in (
    SELECT s.title
    FROM step s
    INNER JOIN step_taken st ON (st.step_id = s.stepID)
    INNER JOIN user u ON (st.user_id = u.userID)
    GROUP BY s.title
    HAVING count(u.userID) - 1 > 5
)

图片说明

结果跟预期不太一样,希望能帮我找出问题出在哪里,并修改一下,谢谢。

重做了一下修改:

先找other users 的数量和对应的 step_num的表
图片说明

再INNERJOIN

SELECT distinct st.user_id, other_users_nums, s.title
FROM step_taken st
INNER JOIN step s ON (st.step_id = s.stepID)
INNER JOIN (SELECT count(distinct(user_id)) - 1 AS other_users_nums,step_id
            FROM step_taken
            GROUP BY step_id
            HAVING other_users_nums >= 5) as other_user
ON (s.stepID = other_user.step_id)

图片说明

这样是不是感觉对了一些

  • 写回答

3条回答 默认 最新

  • zqbnqsdsmd 2019-09-20 09:38
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?