rails
关于join
joinselect count(distinct store_name) from scores left join stores on scores.scoreable_id = stores.id and scores.scoreable_type = 'Store' where store.status = 0 and 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`"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` = '#'
小方法
SUM(CASE WHEN num > 0 THEN 1 else 0 END) AS available_timesROUND('123.654',2) # 123.654 取小数后两位select *, if(status>0,'激活','锁定') AS '操作状态' from car_bind_info_lock_logs where status is not null;
Last updated