MySQL中的产量计算

时间:2013-04-22 14:01:50

标签: mysql wordpress

我正在尝试根据Wordpress meta_values计算收益率,而我正在使用下面的MySQL语句,这不起作用。 SUM(returned.meta_value + staked.meta_value)-SUM(staked.meta_value))/ SUM(staked.meta_value))* 100)不返回任何值。

有人可以帮助我解决下面表达式的错误。

谢谢。

global $post;
$post_author = $post->post_author;
$post_status = 'publish';
$yield = $wpdb->get_var( $wpdb->prepare(
    "
    SELECT (ROUND((((SUM(returned.meta_value+staked.meta_value)-SUM(staked.meta_value))/SUM(staked.meta_value))*100),2),2)
    FROM {$wpdb->posts} p
    JOIN {$wpdb->users} u ON p.post_author = u.ID
    LEFT JOIN {$wpdb->postmeta} staked
        ON p.ID = staked.post_id AND staked.meta_key = 'staked'
    LEFT JOIN {$wpdb->postmeta} returned 
        ON p.ID = returned.post_id AND returned.meta_key = 'returned'
    WHERE p.post_author = %s AND p.post_status = %s
    ",
    $post_author, $post_status
    ) );

1 个答案:

答案 0 :(得分:0)

您的查询没有意义。 select语句使用别名balancebetfrom子句包含别名pmposts

你是说这个吗?

SELECT (ROUND((((SUM(pm.meta_value+pm.meta_value)-SUM(pm.meta_value))/SUM(pm.meta_value))*100),2),2)
FROM {$wpdb->postmeta} pm INNER JOIN
     {$wpdb->posts} p
     ON pm.post_id = p.ID
WHERE pm.meta_key = %s AND pm.meta_key1 = %s AND p.post_author = %s AND
      Month(p.post_date) = MONTH(CURRENT_DATE) AND p.post_status = %s