Solved

Show only latest row from Join

Posted on 2014-03-12
3
280 Views
Last Modified: 2014-03-29
I've got a query which is to pull a username and passwords from 2 different tables, which works great:-
SELECT `_Users`.`User`, Max(`_Users_Passwords`.`Changed`) AS 'Last Changed', `_Users_Passwords`.`Password`
FROM `_Users` INNER JOIN `_Users_Passwords` ON `_Users`.`User` = `_Users_Passwords`.`User`
GROUP BY `_Users`.`User`, `_Users_Passwords`.`Password`;

Open in new window


However it pulls all the passwords relating to each user, what I want to do is only pull the latest row from `_Users_Passwords`identified my `_Users_Passwords`.`Changed` for each user.

I thought I could use MAX which works if its the only field from `_Users_Passwords` however as soon as I put a second in it pulls all the passwords for each user.

Any ideas how I can get the query to only pull one record from the `_Users_Passwords` table which is the max for the user?

Thank you in advance
0
Comment
Question by:tonelm54
3 Comments
 

Author Comment

by:tonelm54
Comment Utility
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I think you might want to change the password-related table to add a column of type TIMESTAMP.  It contains a DATETIME value that will get updated as the row is added or updated.  You can order by this column in DESC and set LIMIT 1 to get the newest password.

You can also use this column to "prune" the table if it grows too big.
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
try this.
SELECT u.*, 
       up.priID, 
       up.dateChanged, 
       up.password 
  FROM users u 
       JOIN (SELECT * 
               FROM (SELECT up.*, 
                            IF(@userID = userID, @rownum := @rownum + 1, @rownum := 1) AS RowNum,
                            IF(@userID <> userID, @userID := userID, 'Dummy')          s 
                       FROM userPasswords up, 
                            (SELECT @rownum := 1, 
                                    @userID := 'Dummy') r 
                      ORDER BY userID, 
                               dateChanged DESC) T1 
              WHERE RowNum = 1) up 
         ON u.userID = up.userID 

Open in new window

http://sqlfiddle.com/#!2/d41a8/17
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now