Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何在Sequel中使用SQL数组切片进行排序?_Ruby_Sequel - Fatal编程技术网

Ruby 如何在Sequel中使用SQL数组切片进行排序?

Ruby 如何在Sequel中使用SQL数组切片进行排序?,ruby,sequel,Ruby,Sequel,按SQL数组值的一部分排序的正确语法是什么 下面是我想在SQL中执行的操作: SELECT a, -- column with SQL array values depth, name FROM nodes ORDER BY a[0:depth], name -- sort by 'a' ignoring last element, then by 'name' 在续集中,我可以做到: DB[:nodes].select(:a, :depth

按SQL数组值的一部分排序的正确语法是什么

下面是我想在SQL中执行的操作:

SELECT
  a,                -- column with SQL array values
  depth,
  name
FROM
  nodes
ORDER BY
  a[0:depth], name  -- sort by 'a' ignoring last element, then by 'name'
在续集中,我可以做到:

DB[:nodes].select(:a, :depth, :name).order(:a, :name)
但这是按所有
:a
进行排序,而不是按需要按
[0:depth]
切片进行排序。正确的语法是什么?

这样做:

DB[:nodes].select(:a, :depth, :name).order('a[0:depth]'.lit, :name)
有更好的建议吗?

这很有效:

DB[:nodes].select(:a, :depth, :name).order('a[0:depth]'.lit, :name)

有更好的建议吗?

这似乎是最好的办法。Sequel的DSL不支持
[0:depth]
语法。您可以执行
:a.sql\u下标('0:depth.lit)
,但这并不能为您带来多少好处。这看起来是最好的方法。Sequel的DSL不支持
[0:depth]
语法。您可以执行
:a.sql\u下标('0:depth.lit)
,但这并不能为您带来多少好处。