• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Subquery alternative

 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a booking table. I would like to query for a column based on the ID and the most recent date.

Here I am using a subquery with aggregate function for the booking_date.

Do I have any alternative here in addition to having a subquery?

I am using MySQL 5.1.

Here is my query,

 
Rob Spoor
Sheriff
Pie
Posts: 20750
68
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that the combination id+booking_date is unique, you can sort by booking_date and select the top 1:
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahsan

I assume the ID field alone is not unique? Otherwise, your query will always return 0 or 1 record(s). Seems obvious but just checking.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic