0

I already have a query like :

SELECT max(cast(meta_value as unsigned)) AS max_racer_nr FROM wp_postmeta WHERE meta_key='racer_nr'

I need to get max racer_nr which is between 1 - 99 or 100 - 199, or 200 - 999 depending on which class racer belongs to.

Igor Yavych
  • 4,166
  • 3
  • 21
  • 42
Scorpioniz
  • 1,421
  • 3
  • 16
  • 36
  • See https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry Sep 24 '17 at 09:20

1 Answers1

1
SELECT MAX(CAST(`meta_value` AS unsigned)) AS `max_racer_nr` FROM `wp_postmeta` WHERE `meta_key` = 'racer_nr' AND `meta_value` BETWEEN 1 AND 99; 

or

SELECT MAX(CAST(`meta_value` AS unsigned)) AS `max_racer_nr` FROM `wp_postmeta` WHERE `meta_key` = 'racer_nr' AND `meta_value` >= 1 AND `meta_value` <= 99; 

You'd just have to change the values in your PHP code depending on the class of the racer. Unless I misunderstand what you need.

Igor Yavych
  • 4,166
  • 3
  • 21
  • 42
  • That's it !!! :) i thought that I need to check each meta_key with values.. not just meta_value.. soo simple it was.. :))) thank you!!! :)) – Scorpioniz Sep 24 '17 at 09:57