I'm trying to create a MySQL statement that will sort by a value calculated within the statement itself. My tables look like this:
posts
+----+-----------+--------------+
| ID | post_type | post_content |
+----+-----------+--------------+
|  1 |      post |        Hello |
|  2 |      post |        world |
+----+-----------+--------------+
postmeta
+---------+----------+------------+
| post_id | meta_key | meta_value |
+---------+----------+------------+
|       1 |    price |         50 |
|       1 |   retail |        100 |
|       2 |    price |         60 |
|       2 |   retail |         90 |
+---------+----------+------------+
I'm trying to calculate a value called savings (.5 for ID=1, .3 for ID=2) then sort by it. This is what I have so far but I'm not sure how to do a calculation across 2 rows (everything I found is about calculating between columns).
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'Price'
AND wposts.post_type = 'post'
ORDER BY wpostmeta.meta_value DESC
Thanks for your help!
 
     
     
    