tags:

views:

38

answers:

3

I am having trouble with the following MYSQL query and are hoping that there is someone that can help me with it.

Here is the query:

SELECT *
FROM dbcasino_db22.tb_content
WHERE content_real_folder = 'foldername/' AND content_no < 5099

This query works but gives me all the records smaller than 5099 that match the content_real_folder - I would actually just want to get the Maximum Result smaller than 5099 and not have the first record matching the content_real folder

My quess is that the end query would look like this - but I am not having JOY with it

SELECT * FROM dbcasino_db22.tb_content WHERE content_real_folder = 'foldername/' AND content_no < 5099 HAVING MAX(content_no)

If there is anybody who could help - it would be much appreciated

Thanks

A: 

I think the simplest solution is to sort by content_no Descending and then LIMIT the responses to 1.

Joe Philllips
+3  A: 

This should work for that. The ORDER tells the max result to be on top and LIMIT 1 tells it you only want the first result

SELECT * FROM dbcasino_db22.tb_content
WHERE content_real_folder = 'foldername/' AND content_no < 5099
ORDER BY content_no DESC
LIMIT 1
Jason
+1 for ORDER BY/DESC/LIMIT
AJ
Thanks a Million Jason I can kick myself for not thinking about the DESC function :-) :-)
Gerald Ferreira
After you've forgotten it a dozen or so times, you'll start to remember. That's how I remembered it so quickly this time.
Jason
@Gerald: If the MAX is 5001 and there are more than 1 record with the value, do you still only want to retrieve one? The result will not be deterministic.
o.k.w
o.k.w thanks for the reply - the content_no is a unique number so it should give me the record that I am looking for - and if it does not exists, then I am hiding the records...
Gerald Ferreira
A: 

if i understand you right, you want

SELECT * FROM dbcasino_db22.tb_content WHERE content_real_folder = 'foldername/' AND content_no < 5099 order by content_no desc limit 1

pfote