Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.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/2/ruby-on-rails/65.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
Mysql 按案例排序条款的范围无法正常工作_Mysql_Ruby On Rails_Named Scope - Fatal编程技术网

Mysql 按案例排序条款的范围无法正常工作

Mysql 按案例排序条款的范围无法正常工作,mysql,ruby-on-rails,named-scope,Mysql,Ruby On Rails,Named Scope,我的rails模型上有一个作用域,用来帮助我对对象进行排序。如下所示: scope :active, ->(u = nil, now = "NOW()") { published_and_private(u).eager_load(:images) .where("(listing = 1 AND complete = 0) OR " + # LISTING "(online_onl

我的rails模型上有一个作用域,用来帮助我对对象进行排序。如下所示:

scope :active, ->(u = nil, now = "NOW()") {
  published_and_private(u).eager_load(:images)
    .where("(listing = 1 AND complete = 0) OR " +                                        # LISTING
         "(online_only = 1 AND scheduled_end_time + INTERVAL 1 DAY >= #{now}) OR " +   # TIMED
         "(online_only = 0 AND listing = 0 AND starts_at + INTERVAL 1 DAY >= #{now})") # LIVE
    .order("complete, CASE WHEN sort_index IS NOT NULL THEN sort_index " +
         "WHEN scheduled_end_time IS NOT NULL THEN scheduled_end_time " +
         "WHEN starts_at IS NOT NULL THEN starts_at ELSE #{now} + INTERVAL 10 YEAR END")
}
以下是运行查询时返回的数据库数据:

select id, name, complete, sort_index, starts_at, scheduled_end_time from auctions where published = 1 ORDER BY complete, CASE WHEN sort_index IS not NULL THEN sort_index WHEN scheduled_end_time IS NOT NULL THEN scheduled_end_time WHEN starts_at IS NOT NULL THEN starts_at ELSE (NOW() + INTERVAL 10 YEAR) END;


+----+-----------------------------------+----------+------------+---------------------+---------------------+
| id | name                              | complete | sort_index | starts_at           | scheduled_end_time  |
+----+-----------------------------------+----------+------------+---------------------+---------------------+
| 21 | Listing: Mountain Cabin Estate    |        0 |          1 | NULL                | NULL                |
| 17 | Multi-Item Online Only            |        0 |          2 | 2017-08-07 06:48:00 | 2017-08-21 12:48:00 |
|  9 | Multi-item Live Auction           |        0 |       NULL | 2017-08-21 18:48:02 | NULL                |
| 19 | Many Item LIVE Auction            |        0 |       NULL | 2017-08-21 18:48:02 | NULL                |
| 10 | Single Item Online Only           |        0 |       NULL | 2017-08-07 18:48:03 | 2017-08-22 00:48:02 |
| 18 | MANY Item Timed Auction           |        0 |       NULL | 2017-08-07 18:48:03 | 2017-08-22 00:48:02 |
| 22 | LISTING: Multi-parcel Real Estate |        0 |       NULL | NULL                | NULL                |
| 20 | Bad Images                        |        0 |          3 | 2017-08-21 14:48:00 | NULL                |
|  8 | Single Item Live Auction          |        1 |       NULL | 2017-08-21 18:48:02 | NULL                |
+----+-----------------------------------+----------+------------+---------------------+---------------------+
我的问题是排序索引为3的对象不合适,任何超过2的数字都会出现这种情况,我完全不知道为什么会这样。我希望查询将该对象放在sort_索引为2的对象下面


如果您有任何帮助、指导或见解,我们将不胜感激。

您是否可以尝试使用
ISNULL
,例如:

.order("complete, ISNULL(sort_index), sort_index, " +
         "ISNULL(scheduled_end_time), scheduled_end_time " +
         "ISNULL(starts_at), starts_at")

没有办法将所有这些条件都放入
案例中。。。当
<代码>案例。。。WHEN有效地在
WHERE
子句中创建一个伪列。因此,你所做的与:

SELECT *, CASE /* your logic */ END AS sort_logic 
/* 
   Notice that the sort_logic column doesn't actually exist in the table. Instead
   MySQL calculates the value and for the duration of this query
*/
WHERE /* <stuff> */
ORDER BY sort_logic
您还可以选择将列值默认为MySQL最大值。在这种情况下,
'9999-12-31'
表示,而
~0


在这种情况下,逻辑是“Sort by
complete
,如果
Sort\u index
不为空,则将其用于排序,否则将结果抛出到列表末尾,其中
Sort\u index
不为空…”,并且所有其他列都遵循相同的逻辑。

您要查找的order子句可能是:

order by complete,
         Coalesce(
           sort_index,
           scheduled_end_time,
           starts_at,
           #{now} + INTERVAL 10 YEAR
         )
但是,您将整数与这种排序中的日期进行比较,因此我不确定这将如何工作——可能是通过隐式类型转换,这不太可能导致期望的结果

也许你的意思是:

order by complete,
         sort_index,
         Coalesce(
           scheduled_end_time,
           starts_at,
           #{now} + INTERVAL 10 YEAR
         )

尝试最后使用
-value
技巧对空值进行排序。我也添加了合并,其他的也一样,这确实清理了案例陈述

ORDER BY 
  complete, 
  -sort_index DESC, -- Sort ascending with nulls last
  COALESCE(scheduled_end_time, starts_at, now() + INTERVAL 10 YEAR)
;

你也可以检查我的

也不太正确。排序索引在这一点上是正确的,但是需要按时间排序的索引的顺序不正确。
scheduled\u end\u time
的排序中是否首先得到空值?如果您仔细想想,您的order by子句没有多大意义。您正在将整数与日期混合。这些应该如何分类?
order by complete,
         sort_index,
         Coalesce(
           scheduled_end_time,
           starts_at,
           #{now} + INTERVAL 10 YEAR
         )
ORDER BY 
  complete, 
  -sort_index DESC, -- Sort ascending with nulls last
  COALESCE(scheduled_end_time, starts_at, now() + INTERVAL 10 YEAR)
;