mysql - 列重命名属于检查mysql查询

标签 mysql ruby-on-rails

我想编写一个类似这样的查询来分别查找硕士生和博士生的奖学金:

select sum(fellowship) as if(ID BELONGS TO #{ms_ids},'ms_fellowship','phd_fellowship') 
from fellowships where id in #{all_ids};

在上面的查询中,我已经使用从另一个表获得的 ruby​​ 代码构建了两个数组 Student_ids 和 Teacher_ids。但问题是我不知道如何编写我想要检查 id 是否属于某个数组(如上面的 Student_ids 数组)的代码部分

最佳答案

加入 ids 数组,假设它是一个像 [3, 53, 43, 43] 这样的数组:

# >>>> #{all_ids.join(",")}

select sum(fellowship) as if(ID BELONGS TO #{ms_ids},'ms_fellowship','phd_fellowship') from fellowships where id in (#{all_ids.join(",")});

关于mysql - 列重命名属于检查mysql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21299706/

相关文章:

php - 登录系统,禁止用户php

ruby-on-rails - 试图让专家授权更干

ruby-on-rails - Rails 问题 : belongs_to with STI -- how do i do this correctly?

php - 在以 PHP 运行的 MySQL 文件中使用 PHP session 变量?

ruby-on-rails - 如何在 Ruby 中将独占范围转换为包含范围?

ruby-on-rails - Rails:ActiveRecord::AssociationTypeMismatch Team() 预期,得到 String()

mysql - 加载错误: cannot load such file -- mysql2/mysql2 - on rake db:create

php - 从本地(100% 工作)迁移到托管时奇怪的 PHP 行为..一些代码工作,一些不工作

php - PDO 更新准备好的语句不起作用

mysql - gorm.DB 可以自动解析外键吗?