如何在 ARel 中使用 CONCAT() 等函数?

编程入门 行业动态 更新时间:2024-10-27 21:24:13
本文介绍了如何在 ARel 中使用 CONCAT() 等函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有办法让 ARel 将(清理过的,可能别名等)列名写入 CONCAT() 和其他 SQL 函数中?

Is there a way to have ARel write (sanitized, possibly aliased, etc.) column names into CONCAT() and other SQL functions?

这里是如何使用 AVG()...

?> name = Arel::Attribute.new(Arel::Table.new(:countries), :name) => #<struct Arel::Attributes::Attribute [...] ?> population = Arel::Attribute.new(Arel::Table.new(:countries), :population) => #<struct Arel::Attributes::Attribute [...] ?> Country.select([name, population.average]).to_sql => "SELECT `countries`.`name`, AVG(`countries`.`population`) AS avg_id FROM `countries`"

(是的,我知道 avg_id 在每一行都是一样的,只是想说明我的问题)

(yes, I know that avg_id would be the same in every row, just trying to illustrate my question)

如果我想要一个不同的功能怎么办?

So what if I wanted a different function?

?> Country.select(xyz).to_sql # Arel::Concat.new(name, population) or something? => "SELECT CONCAT(`countries`.`name`, ' ', `countries`.`population`) AS concat_id FROM `countries`"

谢谢!

推荐答案

Use NamedFunction:

name = Arel::Attribute.new(Arel::Table.new(:countries), :name) func = Arel::Nodes::NamedFunction.new 'zomg', [name] Country.select([name, func]).to_sql

更多推荐

如何在 ARel 中使用 CONCAT() 等函数?

本文发布于:2023-10-26 07:28:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529480.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   如何在   ARel   CONCAT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!