Question

I have a Users table with these values (stripped down for the example):

  • Name
  • City
  • County
  • StateProvince
  • Country
  • Continent
  • Strength
  • Endurance
  • Intelligence

I basically want a query that returns the highest stat by region for yourself. If you have the highest Strength (67) in the country, highest Endurance (59) in your City, and not the highest Intelligence anywhere, I'd like a 2 row result of:

('Strength', 67, 'Country', 'United States')

('Endurance', 59, 'City', 'Redmond')

Note that if you have the highest Strength in the country, you will also have the highest strength in the state/province, county, and city (but I'd like those not to be returned in the result). I can do this all using multiple SELECT queries, however, I'd like to know how to do this in one single query for efficiency (or if that's even a good idea?). I'd imagine the logic would go something like (pseudo code):

(SELECT FROM Users ORDERBY Strength WHERE Country = 'MyCountry' LIMIT 1) IF Name != 'Me' (SELECT FROM Users ORDERBY Strength WHERE StateProvince = 'MyState' LIMIT 1) ... and so on

Furthermore, the above would also need to work with Endurance and Intelligence. Thanks!

Était-ce utile?

La solution

Check this link MySQL control-flow-functions

SELECT CASE 1 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'more' END;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top