0

我有以下查询

select 
    tab1.post_id,
    ((tab1.meta_value - tab2.meta_value) / tab1.meta_value) * 100 as discount
from
    fridaysunday.wp_postmeta as tab1,
    fridaysunday.wp_postmeta as tab2
where
    tab1.post_id = tab2.post_id and tab1.meta_key = 'price' and tab2.meta_key = 'sale_price'
order by discount desc;

我想将此查询与 WP_Query 函数集成。非常感谢您的帮助,在此先感谢!

4

1 回答 1

1
<?php

global $wpdb;
$prefix = $wpdb->prefix;

$query = 'select 
    '.$prefix.'tab1.post_id,
    (('.$prefix.'tab1.meta_value - '.$prefix.'tab2.meta_value) / '.$prefix.'tab1.meta_value) * 100 as discount
from
    '.$prefix.'wp_postmeta as tab1,
    '.$prefix.'wp_postmeta as tab2
where
    '.$prefix.'tab1.post_id = '.$prefix.'tab2.post_id and '.$prefix.'tab1.meta_key = 'price' and '.$prefix.'tab2.meta_key = 'sale_price'
order by discount desc";

$records = $wpdb->get_results($query);



?>
于 2013-05-30T12:25:05.180 回答