freddy
freddy

Reputation: 61

getting last value before i insert a new one from the database

i have a wordpress database table called stats that saves records of one post, actually the data saved is the post score, so i might have post id 1 with score 49, another record for post id 2 with score 59, but the post id is the same, so my question is this,

how will i get the score of the second last post id, in this case 49?

please help, i have searched everywhere and no search answer answer to get this value.

$query = $wpdb->get_row("SELECT * FROM stats WHERE score = MAX(score)");

Upvotes: 0

Views: 41

Answers (1)

Limiter
Limiter

Reputation: 497

one way would be:

$query= $wpdb->get_row("SELECT * FROM stats WHERE score < max(score) ORDER BY score DESC LIMIT 1 ")

Where we order them by score and get the first result that is not the biggest score. Might need some syntax tweaks, I have only done postgreSQL for the last year or so

Upvotes: 1

Related Questions