Warning: mysql_query(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/09/14001309/html/wordpress/wp-content/themes/wardelldesign/header.php on line 103

Warning: mysql_query(): A link to the server could not be established in /home/content/09/14001309/html/wordpress/wp-content/themes/wardelldesign/header.php on line 103

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/content/09/14001309/html/wordpress/wp-content/themes/wardelldesign/header.php on line 107

Warning: implode(): Invalid arguments passed in /home/content/09/14001309/html/wordpress/wp-content/themes/wardelldesign/header.php on line 113
Filtering MySQL Query Results With PHP | Wardell Design Blog

Filtering MySQL Query Results With PHP

By: , On Tuesday, January 27th, 2009

For a while I’d been trying to figure out how to output a mysql query while excluding rows with duplicate values for a specific pair of column values, and then ordering by the most recent timestamp. Some if the phrases I ended up searching the web for were get all from distinct, order by then group by, exclude / remove duplicates, find duplicates, count duplicates and so on.

My ultimate solution was pretty simple and I hope it will help any one else looking for a solution to a similar problem.
$query="SELECT * FROM TABLE01 ORDER BY timestamp DESC";
Arbitrary Code to Execute
}//end while loop

In the above code I created a variable referred to as valuePair containing the pair of values I want to check for duplicates, and an empty array called combinations which I then use to contain the valuePair’s, and check for duplicates with my if statement, if the valuePair is not found in my combination array then the code is executed, if the valuePair is contained in the combination array then the code is not executed, at the end of each while loop iteration, the new valuePair is added to the combination array.

2 Responses to “Filtering MySQL Query Results With PHP”

  1. Andy Bailey Says:

    I sometimes spend an unhealthy amount of time figuring out a long convoluted sql statement, your solution seems to be quite elegant!

    Andy Bailey’s last blog post..Chinese new year with infinity hoovers. video blog post

    Wardell Reply:

    @Andy Bailey, Thank you, and thanks for stopping by. :D

Loading Facebook Comments ...

© Wardell Design 2020
Entries (RSS) and Comments (RSS).