How to get MAX and/or MIN Aggregate mysql function with Propel?

371 Views Asked by At

I'm trying to get the results of this query:

// SELECT MIN(`inventory_date`) FROM `tour_cms_inventory` WHERE extra_id = 52

with this code:

$min = TourCmsInventoryQuery::create()
                    ->withColumn('MIN(inventory_date)')
                    ->filterByExtraId($tour->getId())
                    ->groupByExtraId()
                    ->find();

But I get this error:

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1140 In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'mkte_circuitos_imp.tour_cms_inventory.id'; this is incompatible with sql_mode=only_full_group_by in C:\laragon\www\mkte_circuitos\vendor\propel\propel\src\Propel\Runtime\Connection\StatementWrapper.php:194

I guess that Propel is adding pk (id)(mkte_circuitos_imp.tour_cms_inventory.id) in the select.

Waht I'm doing wrong ?

Best Regards

EDIT I also try this:

$con = \Propel\Runtime\Propel::getWriteConnection(TourCmsInventoryTableMap::DATABASE_NAME);
            $con->useDebug(true);

            $sql = 'SELECT MIN(inventory_date) FROM tour_cms_inventory WHERE extra_id=:extra_id';

            $stmt = $con->prepare($sql);

            $rs = $stmt->execute([':extra_id'=>$tour->getId()]);

but... true is returned !!

1

There are 1 best solutions below

0
Qiniso On

You need an alias for your aggregate function, try:

$min = TourCmsInventoryQuery::create()
   ->withColumn('MIN(inventory_date)', 'MinInventoryDate')
   ->filterByExtraId($tour->getId())
   ->groupByExtraId()
   ->find();