galvin Posted November 13, 2010 Share Posted November 13, 2010 I have a MySQL table with a "dob" field that stores specific dates and times. The data is in this format, for example... 2011-02-03 14:08:00 I want to retrieve the unique YEAR values, as efficiently as possible. I came up with the code below which doesn't work. So my assumption is that you are not able to use "substr" within a MySQL query. Is that right? If so, can someone guide in the direction of the best way to achieve this. I guess I have to just bring back all the dob FULL values into an array, but then not sure how to get just the year (first four characters) and only the UNIQUE ones, since many will be the same year. Or if you actually CAN use "substr" within a MySQL query, then I must have screwed something else up, so let me know what else I might have done wrong. $sql = "SELECT distinct substr(dob,0,4) AS year FROM info WHERE id = '{$_GET['id']}'"; $getyear = mysql_query($sql, $connection); if (!$getyear) { die("Database query failed: " . mysql_error()); } else { while ($years = mysql_fetch_array($getyear)) { echo "Years=" . $years['year']; } } Quote Link to comment https://forums.phpfreaks.com/topic/218594-can-you-use-substr-in-a-mysql-query/ Share on other sites More sharing options...
Pikachu2000 Posted November 13, 2010 Share Posted November 13, 2010 MySQL has a DATE_FORMAT() function. Quote Link to comment https://forums.phpfreaks.com/topic/218594-can-you-use-substr-in-a-mysql-query/#findComment-1133884 Share on other sites More sharing options...
galvin Posted November 14, 2010 Author Share Posted November 14, 2010 Thanks Pikachu2000. I apologize for being a bigtime newbie with MySQL functions but can I stick that function IN the query, or do I call it somewhere else? I tried this, which is obviously not correct :-\ $sql = "SELECT distinct DATE_FORMAT('dob','%Y') AS year FROM data WHERE id = '{$_GET['id']}'"; $getyear = mysql_query($sql, $connection); if (!$getyear) { die("Database query failed: " . mysql_error()); } else { while ($years = mysql_fetch_array($getyear)) { echo "Years=" . $years['year']; } } Quote Link to comment https://forums.phpfreaks.com/topic/218594-can-you-use-substr-in-a-mysql-query/#findComment-1133934 Share on other sites More sharing options...
Pikachu2000 Posted November 14, 2010 Share Posted November 14, 2010 Change the single quotes around dob to `backticks`. You may also need to use parentheses for the DISTINCT function. $sql = "SELECT DISTINCT( DATE_FORMAT(`dob`,'%Y') ) AS year FROM data WHERE id = '{$_GET['id']}'"; Quote Link to comment https://forums.phpfreaks.com/topic/218594-can-you-use-substr-in-a-mysql-query/#findComment-1133957 Share on other sites More sharing options...
PFMaBiSmAd Posted November 14, 2010 Share Posted November 14, 2010 DISTINCT is not a function. DISTINCT() just means that you have put a couple of extra () in your query. Quote Link to comment https://forums.phpfreaks.com/topic/218594-can-you-use-substr-in-a-mysql-query/#findComment-1133961 Share on other sites More sharing options...
Pikachu2000 Posted November 14, 2010 Share Posted November 14, 2010 OK, thanks for clearing that up. Quote Link to comment https://forums.phpfreaks.com/topic/218594-can-you-use-substr-in-a-mysql-query/#findComment-1133962 Share on other sites More sharing options...
galvin Posted November 14, 2010 Author Share Posted November 14, 2010 The backticks did it! Thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/218594-can-you-use-substr-in-a-mysql-query/#findComment-1133987 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.