How to select DISTINCT with Datatables 1.10.4 ?

How to select DISTINCT with Datatables 1.10.4 ?

zlenzlen Posts: 5Questions: 2Answers: 1
edited December 2014 in Free community support

I havn't found the answer in the forums.

I have two columns named col1 and col2 in my database, and I would like to "SELECT DISTINCT col1 FROM ..."

Is there a way to do this with DataTables 1.10.4 ?

I use the SSP class via this url : http://www.datatables.net/examples/data_sources/server_side.html

This question has an accepted answers - jump to answer

Answers

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited December 2014 Answer ✓

    You do not have to use the ssp.class.php in that example; it is simply an example. The only requirement is the return.

    return array(
        "draw"            => intval( $request['draw'] ),
        "recordsTotal"    => intval( $recordsTotal ),
        "recordsFiltered" => intval( $recordsFiltered ),
        "data"            => $data
    );
    

    But it looks like you could change line 220 from:

    "SELECT SQL_CALC_FOUND_ROWS `".implode("`, `", self::pluck($columns, 'db'))."`
    

    to

    "SELECT DISTINCT `".implode("`, `", self::pluck($columns, 'db'))."`
    

    The <code> and </code> tag is just a tilde, I'm not sure why that happens inside the three tilde chunk. Also, if you make multiple tables with more complicated queries it would definitely be worth writing your own class file.

  • zlenzlen Posts: 5Questions: 2Answers: 1
    edited December 2014

    Thank you for your quick reply !
    I am very reluctant as to change the classes of developer.

    To achieve my goal, I need to do a select distinct first and then a join. But your answer helped me ! Thanks a lot !

    Then, if I need to join with others tables, I use this modified SSP class : https://github.com/emran/ssp/blob/master/ssp.php

  • allanallan Posts: 63,530Questions: 1Answers: 10,473 Site admin

    Perhaps modifying the class to add a distinct() method that would add the DISTINCT condition tot he SQL would be a good way of doing it.

    Allan

This discussion has been closed.