DAVID_ASC Posted August 11, 2009 Share Posted August 11, 2009 Hi All, I have tried to create a list from a table in my database table. I have a simple query that runs when a user logs in to my site that adds the user name and a date/timestamp into two fields (user_name and date). Now what I want is for a page that I can see to show me the last 15 login's to the site. Now I have managed to get tis query to run fine by using this query: SELECT * FROM loggedon ORDER BY date DESC LIMIT 0, 15 Now what I want is for the list to only show the latest from each username with a maximum of 15 items displayed as the above shows a list of the last 15 login's but some of these could be from the same user. Can anyone show me the code that might do this. I have tried using a "SELECT DISTINCT" code but this seems to do nothing. as shown below. This just shows the same information as if the "DISTINCT" was not there. SELECT DISTINCT user_name, date FROM loggedon ORDER BY date DESC LIMIT 0, 15 Any help??? Quote Link to comment https://forums.phpfreaks.com/topic/169813-select-distinct-query/ Share on other sites More sharing options...
kickstart Posted August 11, 2009 Share Posted August 11, 2009 Hi The distinct will bring back distinct rows, and the different date will make them non distinct. Try this:- SELECT user_name, MAX(`date`) AS LastLoginDate FROM loggedon GROUP BY user_name ORDER BY LastLoginDate DESC LIMIT 0, 15 All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/169813-select-distinct-query/#findComment-895867 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.