0

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();
    }
}

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.