Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get unique values of SQL table and echo with PHP

I have a MySQL table with over 200 values. One of the columns on my table is 'date'. Out of all 200 values there are only 5 unique dates.

How can I list out the unique values of the dates and echo them with php. e.g. not getting back 200 instances of dates but just 5.

like image 432
Philip Kirkbride Avatar asked Dec 21 '22 20:12

Philip Kirkbride


2 Answers

Use DISTINCT

SELECT DISTINCT `date` FROM `tablename`....
like image 99
John Conde Avatar answered Dec 30 '22 11:12

John Conde


SELECT myDate FROM myTable GROUP BY myDate

Or...

SELECT DISTINCT myDate FROM myTable

DISTINCT is a nice short hand, but if you ever then want to make use of the query for other purposes, if often constrains you a bit too much. So I prefer the GROUP BY version.

like image 26
MatBailie Avatar answered Dec 30 '22 09:12

MatBailie