在制作某炒股网站时,客户提出要统计一下,用户打赏的总和,同时还有分出本月,本周,当天以及上个月的打赏总和。在wordpress本身函数中并没有直接的方法,只能通过数据库查询来解决。

那么wordpress如何进行数据库查询呢?


首先是,查询时间类的sql:

1、查询当天的数据  
  
select * from 表名 where TO_DAYS(时间字段)=TO_DAYS(NOW());  
  
2、查询当周的数据  
  
select * from 表名 where YEARWEEK(DATE_FORMAT(时间字段,'%Y-%m-%d'))=YEARWEEK(NOW());  
  
3、查询当月的数据  
  
select * from 表名 where DATE_FORMAT(时间字段,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m');  
  
4、查询昨天的数据  
  
select * from 表名 where TO_DAYS(NOW())-TO_DAYS(时间字段)=1;  
  
5、查询最近7天的数据  
  
select * from 表名 where DATE_SUB(CURDATE(),INTERVAL 7 DAY)<=DATE(时间字段);  
  
6、查询当年的数据  
  
select * from 表名 where YEAR(时间字段) =YEAR(NOW());  
  
7、查询上周的数据  
  
select * from 表名 whereYEARWEEK(DATE_FORMAT(时间字段,'%Y-%m-%d'))=YEARWEEK(NOW())-1;  
  
8、查询上月的数据  
  
select *from 表名where PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(时间字段,'%Y%m'))=1;  

然后,只要加上条件就可以了:

总  
SELECT SUM(order_total_price) AS CustomerNilsen FROM `mu_orders`WHERE product_id=-6 AND order_status=4  
今天  
SELECT SUM(order_total_price) AS CustomerNilsen FROM `mu_um_orders`WHERE product_id=-6 AND order_status=4 AND TO_DAYS(order_success_time)=TO_DAYS(NOW())  
本月  
SELECT SUM(order_total_price) AS CustomerNilsen FROM `mu_um_orders`WHERE product_id=-6 AND order_status=4 AND  DATE_FORMAT(order_success_time,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m');  
上月  
SELECT SUM(order_total_price) AS CustomerNilsen FROM `mu_um_orders`WHERE product_id=-6 AND order_status=4 AND PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(order_success_time,'%Y%m'))=1;  

其中,我的数据表里,productid=-6是打赏数据,status=4是成功付款的。

最后是应用到wordpress:

function.php里添加

function displays(){  
global $wpdb;  
$prefix = $wpdb->prefix.'um_orders';  
  
  
    $zsr = $wpdb->get_var("SELECT SUM(order_total_price) AS CustomerNilsen FROM `mu_um_orders`WHERE product_id=-6 AND order_status=4");  
    $ysr = $wpdb->get_var("SELECT SUM(order_total_price) AS CustomerNilsen FROM `mu_um_orders`WHERE product_id=-6 AND order_status=4 AND  DATE_FORMAT(order_success_time,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m')");  
    $dsr = $wpdb->get_var("SELECT SUM(order_total_price) AS CustomerNilsen FROM `mu_um_orders`WHERE product_id=-6 AND order_status=4 AND TO_DAYS(order_success_time)=TO_DAYS(NOW())");  
    $qsr = $wpdb->get_var("SELECT SUM(order_total_price) AS CustomerNilsen FROM `mu_um_orders`WHERE product_id=-6 AND order_status=4 AND PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(order_success_time,'%Y%m'))=1");  
?>  
  <div class="wrap">  
    <h2>收入统计</h2>  
<table class="form-table">  
    <tbody>  
        <tr>  
            <th scope="row">总收入</th><td><?php echo $zsr; ?></td>  
        </tr>  
        <tr>  
            <th scope="row">本月</th><td><?php echo $ysr; ?></td>  
        </tr>  
        <tr>  
            <th scope="row">当天</th><td><?php echo $dsr; ?></td>  
        </tr>  
        <tr>  
            <th scope="row">上月</th><td><?php echo $qsr; ?></td>  
        </tr>  
        <tr>  
            <th></th><td></td>  
        </tr>  
    </tbody>  
</table>  
  
  </div>  
<?php  
}  

如此,就完成了。