Avoiding duplicates of a specific column in database

I have the following method for pulling out all the data from my database. However in my database there is a column called media_id. I want to make sure that it only fetches data where the media_id is unique. If there is a duplicate based on the media_id column, it just needs to pick the first one it runs into and disregard all the rest. Anyone have any suggestions how I do this? Any help is greatly appreciated.

public function getAllValuesFromOneTable($table1, $fromDate, $toDate) {
    try {
        $statement = $this->prepareStatement("SELECT * FROM $table1 WHERE date_of_post >= '$fromDate' AND date_of_post <= '$toDate'");
        $statement->execute();
        $test = $statement->fetchAll(PDO::FETCH_ASSOC);
        return $test;
    } catch (Exception $e) {

        echo "error: " . $e->getMessage();
    }
}

Upvotes: 0

Views: 24

Answers (2)

PSVSupporter
PSVSupporter

Reputation: 348

Use

select distinct media_id 

(and if the filename is in a separate field, add this column to the query) This way if the table would be

12
54
234
65
12
234

It only returns:

12
54
65
234

Upvotes: 1

Hassan Al Bourji
Hassan Al Bourji

Reputation: 195

you can group your results in sql search by media_id

SELECT * FROM $table1 WHERE date_of_post >= '$fromDate' AND date_of_post <= '$toDate' group by media_id

Upvotes: 0

Related Questions