Closed Brobin closed 3 years ago
I was looking at the code a little and it looks like the
select_context($table, $join, &$columns = null, $where = null, $column_fn = null)
function already has the functionality for a column function ($column_fn
)
It is just set to null, and is never passed in from anywhere. @catfan, were you planning on implementing this in the future?
It will be considerable, but I don't want to make it more complex. It have to spent more time to think around.
The $column_fn
is used on count()
, max()
, min()
functions and more.
This is how i did it. under the comment for variables add the line protected $distinct_mode = false;
then i added the folowing function below the debug function
public function distinct()
{
$this->distinct_mode = true;
return $this;
}
to the function select_context($table, $join, &$columns = null, $where = null, $column_fn = null) I added:
if($this->distinct_mode)
{
$distinct = 'DISTINCT ';
} else {
$distinct = '';
}
and changed the return line (last line in the function) to
return 'SELECT ' . $distinct . $column . ' FROM ' . $table . $this->where_clause($where);
Then it can be set the same way as using the debug() function ie
$database->distinct()->select(... $database->debug()->distinct()->select(...
hope this helps for anyone in the future
@darre4nbrown thanks mate
@darre4nbrown thanks, but in latest version the function should be return 'SELECT ' . $distinct . $column . ' FROM ' . $table_query . $this->where_clause($where);
Another workaround:
$result = array_unique($database->select($table, $columns));
This is how you can select distinct values - by passing SQL functions using Medoo::raw
$resultset = $db->select(
// table name
'posts',
// Columns
[
'month' => Medoo::raw( 'DISTINCT (month' )
],
);
Hi, how about the distinct feature implement? Could we use it in the latest version?
Hi, how about the distinct feature implement? Could we use it in the latest version?
Any progress on this?
I'd love to see this implemented too.
use Medoo::raw like this:
$where = []; # some conditions. $db->select('table', ['field_name'=>Medoo\Medoo::raw('DISTINCT field_name')], $where);
For example, I would like to run a query like this.
SELECT DISTINCT(month), year FROM posts
I haven't found anything in the documentation that addresses this. Perhaps the syntax could be like this:
$medoo->select("posts", array("month", "year"), array("DISTINCT" => "month"));
or:$medoo->select("posts", array("DISTINCT month", "year"));
or:$medoo->select("posts", array("DISTINCT(month)", "year"));
I don't think this would be too difficult to implement. I would be willing to help out, but I need to study the codebase more carefully first.