0

so we have the mysql MIN() function for getting the minimum in a table...but what if I want to get a MIN() based on a criteria

so something like MIN(age) WHERE height < 170

in which the aim is to get the minimum age of ONLY people whose height is < 170...so say sam is the youngest of those with height < 170, but pam is younger than sam but have height > 170, then the query should return sam instead of pam...

how would I compose such query?

1
  • You mean the query will return 23 (Sam's age), right? Commented Apr 17, 2011 at 21:33

1 Answer 1

3

You say you want the "minimum age of ONLY people whose height is < 170"

SELECT MIN(age)
FROM YourTable
WHERE height < 170

Will in fact work as you require then!

WHERE limits row pre aggregation, HAVING can be used to filter on the results of aggregations.

Sign up to request clarification or add additional context in comments.

2 Comments

You mean select name from yourtable where height < 170 having min(age) otherwise "Pam" will still not show up :-)
@Johan - No, I was very careful to quote the phrase in the OP's question that my answer pertains to. If they do want the name as well they should just use WHERE, ORDER BY and LIMIT to get the first row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.