catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

[Feature Request] Allow DISTINCT in a select statement #113

Closed Brobin closed 3 years ago

Brobin commented 10 years ago

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.

Brobin commented 10 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?

catfan commented 10 years ago

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.

darre4nbrown commented 9 years ago

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

ubermanu commented 9 years ago

@darre4nbrown thanks mate

dimitrycastex commented 8 years ago

@darre4nbrown thanks, but in latest version the function should be return 'SELECT ' . $distinct . $column . ' FROM ' . $table_query . $this->where_clause($where);

repat commented 7 years ago

Another workaround:

$result = array_unique($database->select($table, $columns));
adarshmadrecha commented 5 years ago

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' )
      ],
    );
cw1427 commented 5 years ago

Hi, how about the distinct feature implement? Could we use it in the latest version?

nathanael540 commented 5 years ago

Hi, how about the distinct feature implement? Could we use it in the latest version?

scr4bble commented 4 years ago

Any progress on this?

SevenbitsIt commented 4 years ago

I'd love to see this implemented too.

meast commented 3 years ago

use Medoo::raw like this:

$where = []; # some conditions. $db->select('table', ['field_name'=>Medoo\Medoo::raw('DISTINCT field_name')], $where);