Question

Hello I have a sql syntax and it always take the last record and doest check the other condition.

SELECT * 
from projetstaches ,users,timesheets 
WHERE  `prtTimeSheetId` = ( SELECT MAX(  `prtTimeSheetId` ) FROM projetstaches )  AND usrId = 16 AND timId = prtTimeSheetId

I'm working with php and sql but I know this is my syntax is not good.

It's always give me my last record . It's do not take the last record of my user 16 . Cause my last record its for my user 7 . Have any idea why?

So I need to take the last projettime sheet of my user 16

**EDIT ** here what look like my data http://pastebin.com/6LBwGtc3

Was it helpful?

Solution

I suppose your query should look like this:

SELECT * 
from
    projetstaches
        inner join
    timesheets
        on (timesheets.timId = projetstaches.prtTimeSheetId)
        inner join
    users
        on (users.usrId = timesheets.timUserId)
WHERE
    users.usrId = 16
order by timesheets.timId desc
limit 1

To understand how it works I suggest you to play with this query:

  1. remove where - check result
  2. remove limit - check result
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top