Propel ORM including MAX in criteria

混江龙づ霸主 提交于 2019-12-13 13:44:22

问题


I am writing a query using the Propel ORM

The query is of the form:

select * from some_table where some_table.created_at = (SELECT MAX(some_table.created_at) from some_table);

I got this far:

 $c = new Criteria();
 $c->addSelectColumn('MAX('.self::CREATED_AT.')');

Anyone knows how to use Propel to do this, to save me writing RAW SQL?


回答1:


Try:

$c = new Criteria();
$c->add(SomeTable::CREATED_AT, '(SELECT MAX('.SomeTable::CREATED_AT.') FROM some_table)', Criteria::CUSTOM);



回答2:


If you jhust want to know how to add custom WHERE values, then the solution by @prodigitalson should work, but I wonder why you are doing it this way in the first place versus just:

$recs = SomeTableQuery::create()->orderByCreatedAt()->findOne();

...which will get you the latest created record.




回答3:


This way is not mentioned - so this works for me:

MyTable::create() 
->select('max_id') 
->addAsColumn('max_id', 'MAX(id)')
->findOne();

find() returns a collection of objects so use findOne()

addAsColumn() returns just the selected column.




回答4:


I couldn't get ->withColumn('MAX(itemrevisionID)') to work, so the work around was this ->orderByitemrevisionID('desc')



来源:https://stackoverflow.com/questions/1868673/propel-orm-including-max-in-criteria

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!