Black Mamba
Black Mamba

Reputation: 25

(MYSQL) Selecting last row with specific parameters

I'm having trouble with a PHP project I was given. More particularly the MYSQL database is troubling me.. Here is the idea of the whole thing:

If the End radio button is pressed, I want the following to happen: find the LAST row with the selected in form names for emp and worker (they input codes in the form which are checked and the actual names are stored as variables). So is it possible to make something like:

SELECT ID FROM Log ORDER BY ID DESC LIMIT 1
WHERE Worker_Name='$nameW' and Employer_Name='$nameE'

I say something like it, because it gives me an error in the whole WHERE part and does not accept my query..

Any help would be appreciated. Thanks in advance!

Upvotes: 0

Views: 334

Answers (2)

Wolfack
Wolfack

Reputation: 2769

Your query is not correct you have to put the order by and the limit part at the end (i.e. after the where condition). The correct query for your situation will be:

SELECT ID FROM Log 
WHERE Worker_Name='$nameW' and Employer_Name='$nameE' ORDER BY ID DESC LIMIT 1

Upvotes: 1

Satender K
Satender K

Reputation: 581

Your query is wrong use it as below :

 SELECT `ID` FROM `Log` 
 WHERE `Worker_Name`='{$nameW}' and `Employer_Name`='{$nameE}'
 ORDER BY `ID` DESC LIMIT 1

Upvotes: 1

Related Questions