Jump to content

Recommended Posts

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???

Link to comment
https://forums.phpfreaks.com/topic/169813-select-distinct-query/
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.