ClanCats / Hydrahon

🐉 Fast & standalone PHP MySQL Query Builder library.
https://clancats.io/hydrahon/master/
MIT License
278 stars 58 forks source link

Native support for aggregate functions #2

Closed lucas-facchini closed 9 years ago

lucas-facchini commented 9 years ago

I have several statements that require the use of COUNT and SUM in my project. Since the library parses my input, I have to use the raw(expression) function which solves the problem, but it doesn't look good in the code.

Edit: There's a function count() but it doesn't let you choose the column..

mario-deluna commented 9 years ago

Hello i've added a sql function system.

You have now following native helpers:

// simple count
$table->select()->count();

// with special field
$table->select()->count('id');

// min value
$table->select()->min('views');

// max value
$table->select()->max('views');

// sum value
$table->select()->sum('views');

// avarage value
$table->select()->avg('views');

In more complex queries you can make use of the new field helpers:

$query->addFieldCount('id');

// with alias
$query->addFieldCount('id', 'total_count');

// max 
$query->addFieldMax('views');

// min
$query->addFieldMin('views');

// sum
$query->addFieldSum('views');

// avarage
$query->addFieldAvg('views');

// rounding
$query->addFieldRound('price', 2, 'rounded_price');

See: