Thursday 15 August 2013

MySQL sort by calculated value of 2 rows -


"itemprop =" text ">

I am trying to create a MySQL statement that will be sorted based on a value calculated within the statement My table will look like this:

  posts + ---- + ----------- + -------------- + | ID | Post_type | Post_ content | + ---- + ----------- + -------------- + | 1 | Posted in | Hello | 2 | Posted in | World | + ---- + ----------- + -------------- + Postman + --------- + ----- ----- + ------------ + | Post_id | Meta_key | Meta_Luite | + --------- + ---------- + ------------ + | 1 | Price | 50 | | 1 | Retail | 100 | | 2 | Price | 60 | | 2 | Retail | 90 | + --------- + ---------- + ------------ +  

I'm trying A value savings named am ( .5 for id = 1 , .3 for id = 2 ) So to calculate it by order it's the same I've done so far, but I'm not sure how to calculate a 2 in the rows (everything is about calculating between the columns I found).

  Select wposts * $ Wpdb-to & gt; Posts wposts, $ wpdb-> Wpostmeta.meta_value DESC by postmeta wpostmeta wposts.id = wpostmeta.post_id and wpostmeta.meta_key = 'value' and wposts.post_type = 'post' order  

Thank you for your help!

Here is a simple join that will calculate the per post savings, which assumes that meta_value one There is a numerical data type.

  Choose from posts.id, (retail.meta_value - price.meta_value) * 1.0 / retail.meta_value as a post-saving, (select * from postmeta) where meta_key = 'price') As a value, (where select 'meta_key =' retail 'from postmeta) as retail, where post. Id = price.post_id and posts.id = retail.post_id and posts.post_type =' post 'order deposit capital ; + ---- + --------- + | ID | Savings | + ---- + --------- + | 1 | 0.50000 | | 2 | 0.33333 | + ---- + --------- +  

No comments:

Post a Comment