0

I'm having issues writing a MySQL query. It's actually a pretty large one, and the part I'm having issues with is about this User_City table :

| user_id | int(11)
| city_id | int(11)
| from    | date
| to      | date

I would like to compare the ID of the city in which the user we're looking for currently lives, with the ID provided by the user performing the research. I have something like this :

User_City.user_id=User.id and User_City.city_id='.$city_id.' and...

Now the problem is writing the last part where I specify that I want to compare the ID of the most recent entry, meaning the line in which the from field has the highest value but the user_id is still the same.

user1319182
  • 481
  • 12
  • 26
  • `ORDER BY from DESC LIMIT 1` will get the most recent row that matches the criteria. – Barmar May 25 '17 at 01:25
  • If you're getting results for multiple users and you need the max row for each, see http://stackoverflow.com/questions/7745609/sql-select-only-rows-with-max-value-on-a-column?rq=1 – Barmar May 25 '17 at 01:26

0 Answers0