我想编写一个类似这样的查询来分别查找硕士生和博士生的奖学金:
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/