質問

Hello I want to get a specific userid who enter specific data. When X user enter data and save h will be the last row. But How I take the last row of exemple my user id = 16. Mnay user can insert data after him but I want only the last row of my user id 16.

SELECT * 
from
    projetstaches
        inner join
    timesheets
        on (timesheets.timId = projetstaches.prtTimeSheetId)
        inner join
    users
        on (users.usrId = timesheets.timUserId)
WHERE
    users.usrId = 16  AND `prtTimeSheetId` = ( 
SELECT MAX(  `prtTimeSheetId` ) 
FROM projetstaches ) 

When I do something like this it's return blank cause the last user who entered something is 7.

So how do I retrieve my last user = 16?

Here what look my table ( this is a old ver but good to know how it's look like the data now is more completed but the name of table its the same)

http://pastebin.com/6LBwGtc3

役に立ちましたか?

解決

The subquery needs to use a join to select the highest prtTimeSheetId for this user, not the entire table.

SELECT * 
from
    projetstaches
        inner join
    timesheets
        on (timesheets.timId = projetstaches.prtTimeSheetId)
        inner join
    users
        on (users.usrId = timesheets.timUserId)
WHERE
    users.usrId = 16 
AND prtTimeSheetId = (
    SELECT MAX(preTimeSheetId)
    from 
        projetstaches
            inner join
        timesheets
            on (timesheets.timId = projetstaches.prtTimeSheetId)
            inner join
        users
            on (users.usrId = timesheets.timUserId)
    WHERE users.usrId = 16)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top