我有以下查询:
select distinct db
from aliasnames
where concat(trim(group), ' ', trim(subgrp)) like 'My Client'
我如何使用Ruby的续集来做到这一点?
答案 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同时具有CONCAT和TRIM,因此您应该可以执行以下操作:
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'")