使用Concat和Trim与Ruby的续集

时间:2014-05-05 13:12:48

标签: ruby sequel

我有以下查询:

select distinct db
from aliasnames
where concat(trim(group), ' ', trim(subgrp)) like 'My Client'

我如何使用Ruby的续集来做到这一点?

2 个答案:

答案 0 :(得分:1)

DB[:table].select(Sequel.join([:a,'b','c'],',')).sql
=> "SELECT CONCAT(`a`, ',', 'b', ',', 'c') FROM `table`"

DB[:table].select(Sequel.join([:a,'b','c'])).sql
=> "SELECT CONCAT(`a`, 'b', 'c') FROM `table`"

DB[:table].select(Sequel.trim(:a)).sql
=> "SELECT trim(`a`) FROM `table`"

DB[:table].select { concat(a,b,c) }.sql
=> "SELECT concat(`a`, `b`, `c`) FROM `table`"

DB[:table].select { concat(trim(a),b,c) }.sql
=> "SELECT concat(trim(`a`), `b`, `c`) FROM `table`"

答案 1 :(得分:0)

您使用的是哪种SQL DB? MySQL同时具有CONCATTRIM,因此您应该可以执行以下操作:

SELECT DISTINCT db FROM aliasnames WHERE CONCAT(TRIM(group), ' ', TRIM(subgrp)) LIKE 'My Client'

PostgreSQL也是如此 - 这里是string functions in their documentation的链接。

更新: 我对Sequel工具包不太熟悉,但看起来你可以像这样包装你的查询:

results = DB.fetch("SELECT DISTINCT db FROM aliasnames WHERE CONCAT(TRIM(group), ' ', TRIM(subgrp)) LIKE 'My Client'")