gpt4 book ai didi

mysql - ActiveRecord - 按字段排序在最后调用时抛出 StatementInvalid

转载 作者:太空宇宙 更新时间:2023-11-03 16:56:32 26 4
gpt4 key购买 nike

创建一个 activerecord 查询并调用 last,它有一个使用 MYSQL 的 order by field 的订单会抛出一个 StatementInvalid 异常。

例如:

ruby-1.9.2-p180 > User.order('field(name, \'joe\')').last

ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right
syntax to use near 'DESC, "joe") DESC LIMIT 1' at line 1:
SELECT `users`.* FROM `users` ORDER BY field(name DESC, "joe") DESC LIMIT 1

问题是 activerecord 将 DESC 附加到 field 语句内部和外部的 name

有没有更好的方法来使用 activerecord 按特定列值排序,或者有解决此问题的方法?

最佳答案

怎么了

User.where("name LIKE ?", \'joe\').last!

如果您只想要最后一条记录,为什么还要使用 order 呢?

关于mysql - ActiveRecord - 按字段排序在最后调用时抛出 StatementInvalid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7891122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com