selectcount(distinct store_name) from scores left join stores on scores.scoreable_id = stores.id and scores.scoreable_type ='Store'where store.status =0and scores.user_type=0;# 这个是查询 所有带有评分的门店,并且门店是营业状态(0)和评分的类型是用户评分(0)
User.joins(:roles).to_sql# => "SELECT `users`.* FROM `users` INNER JOIN `users_roles` ON `users_roles`.`user_id` = `users`.`id` INNER JOIN `roles` ON `roles`.`id` = `users_roles`.`role_id`"
Inner join 是左右表等价的.
includes
A.includes(:bs).where(bs: {name:'#'}).count# =># SELECT COUNT(DISTINCT `bs`.`id`) FROM `as` LEFT OUTER JOIN `bs` ON `bs`.`a_id` = `as`.`id` WHERE `bs`.`name` = '#'