Skip to content

Instantly share code, notes, and snippets.

@nexneo
Created January 11, 2013 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nexneo/4507450 to your computer and use it in GitHub Desktop.
Save nexneo/4507450 to your computer and use it in GitHub Desktop.
Let me create some sample data, to show you why:
mid :: mday_no :: something
1 :: 37 :: apple
2 :: 37 :: banana
2 :: 38 :: canteloupe
Now consider the first query:
SELECT * FROM cb_main WHERE mid = '1' AND mday_no=(select max(mday_no))
Okay, the select max(mday_no) will return a value of 38, right? So then that query becomes the same as
SELECT * FROM cb_main WHERE mid = '1' AND mday_no = 38
And you can clearly see that there are NO RECORDS AT ALL that match that query. So you will get a blank recordset.
Now compare that to:
SELECT * FROM cb_main WHERE mid = '1' ORDER BY mday_no DESC limit 1
That *WILL* return the record
1 :: 37 :: apple
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment