Pri*_*ams 3 mysql grails grails-orm findby
我在 SO 和其他地方查看了这个问题的一堆答案,但我能找到的只是人们只想找到最高 id、最大 dateCreated 或最新数据库条目的情况,但我想做的是检索创建的最新对象也符合另一个条件。我的域类具有以下属性:id、number、company、type和dateCreated。content该company属性只能设置为“OYG”或“BAW”,并且该number属性是一个自动递增的 int。number我想要做的是检索属性设置为“OYG”或“BAW”的最高记录company。
这是一个例子:
+----------------------------------------------------------+
| id | number | company | type | dateCreated | content |
+----------------------------------------------------------+
| 1 | 0 | OYG | TsAndCs | 15/09/2016 | stuff |
| 2 | 0 | BAW | TsAndCs | 15/09/2016 | stuff |
| 3 | 1 | OYG | TsAndCs | 16/09/2016 | stuff |
| 4 | 2 | OYG | TsAndCs | 17/09/2016 | stuff |
| 5 | 1 | BAW | TsAndCs | 16/09/2016 | stuff |
+----------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
我想说 def doc = Document.findBy Highest NumberAndCompany('OYG') 那么它应该带回 id 4 的对象。 def doc = Document.findBy Highest NumberAndCompany('BAW') 应该带回 id 5 的对象,等等。
任何帮助,将不胜感激。谢谢!
尽管 Joshua Moore 为您提供了一个很好的解决方案,但在一行中还有另一个更简单的解决方案。
MyDomain.findAllByCompany(company, [sort: 'number', order: 'desc', limit: 1])?.first()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1732 次 |
| 最近记录: |