mysql - 如何使用子选择?

标签 mysql subquery

如何使用 MySQL 中的子选择查询或任何其他方法重写此查询?
我使用 3 个表:

SELECT i.resp_id as id
     , Count(1) 
FROM int_result i 
    JOIN response_set rs on i.response_set_id = rs.id 
    JOIN cx_store_child cbu on rs.cx_business_unit_id = cbu.child_bu_id
         AND cbu.business_unit_id = 30850
         AND rs.survey_id IN (5550512,5550516,5550521,5550520,5590351,5590384,5679615,5679646,5691634,5699259,5699266,5699270)
         AND i.q_id IN (52603091,52251250,52250724,52251333,52919541,52920117,54409178,54409806,54625102,54738933,54739117,54739221)
         AND rs.t >= '2017-08-30 00:00:00'
         AND rs.t <= '2017-09-30 00:00:00'
GROUP BY rs.cx_business_unit_id, i.a_id;

最佳答案

也许是这样?

SELECT i.resp_id as id
     , Count(1) 
FROM int_result i, response_set rs, cx_store_child cbu
WHERE i.response_set_id = rs.id
         AND rs.cx_business_unit_id = cbu.child_bu_id
         AND cbu.business_unit_id = 30850
         AND rs.survey_id IN (5550512,5550516,5550521,5550520,5590351,5590384,5679615,5679646,5691634,5699259,5699266,5699270)
         AND i.q_id IN (52603091,52251250,52250724,52251333,52919541,52920117,54409178,54409806,54625102,54738933,54739117,54739221)
         AND rs.t >= '2017-08-30 00:00:00'
         AND rs.t <= '2017-09-30 00:00:00'
ORDER BY rs.cx_business_unit_id, i.a_id; 

关于mysql - 如何使用子选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46523723/

相关文章:

php - MySQL INSERT 只插入 foreach 循环的第一行

mysql - 如何从同一查询中的另一列中选择数据?

mysql - 子查询或连接以匹配值范围内 2 个字段上的 2 个表

mysql - 将 2 个 SQL 查询组合在一起时出错

php - Laravel-5.6 'LIKE ' 在 where 和 or where select

c# - Fluent NHibernate QueryOver 选择不在另一个表中的项目(左连接)

MySQL 错误 1093 - 无法在 FROM 子句中指定要更新的目标表

subquery - Sequelize 使用急切加载的模型创建子查询的顶级

MySQL 连接,查询未结束(循环?)

mysql - 将评论与自己联系起来