最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wpdb - PHP | left join avg query give me Array()

programmeradmin1浏览0评论

I'm trying to implement a query within my code that gives me the ability to calculate the average value between the ratings received and the number of ratings per post, and sort posts from the one with the highest rating to to the one with the lowest average rating

looking at the following table and implementing this query everything works perfectly.

   <?php
    $results = $wpdb->get_results("
    SELECT comment_id, avg(meta_value) avg_meta_value
    FROM {$wpdb->prefix}commentmeta 
    
    
    WHERE {$wpdb->prefix}commentmeta.meta_key = 'rating'
    GROUP BY comment_id 
    ORDER BY avg_meta_value desc");
    foreach ($results as $result)
    {
        echo $result->comment_id.'<br>';
    }

the problem happens when I want to get a left join with the comments table:

even if the query is correct:

        <?php
        $results = $wpdb->get_results("
        SELECT comment_id, avg(meta_value) avg_meta_value
        FROM {$wpdb->prefix}commentmeta 
        
        LEFT JOIN $wpdb->prefix}comments
        ON {$wpdb->prefix}commentmetament_id = $wpdb->prefix}commentsment_ID
        
        
        WHERE {$wpdb->prefix}commentmeta.meta_key = 'rating'
        GROUP BY comment_id 
        ORDER BY avg_meta_value desc");
        
        foreach ($results as $result)
        {
            echo $result->comment_id.'<br>';
        }
        ?>

doing the print_r($ results); i get: Array( )

and I don't understand why, understanding this step is essential for me to make the next join with the post table, so that I can get all the values ​​out, why do I get array ()?

how can i fix it?

.... and

var_dump($result); return me `NULL`

I'm trying to implement a query within my code that gives me the ability to calculate the average value between the ratings received and the number of ratings per post, and sort posts from the one with the highest rating to to the one with the lowest average rating

looking at the following table and implementing this query everything works perfectly.

   <?php
    $results = $wpdb->get_results("
    SELECT comment_id, avg(meta_value) avg_meta_value
    FROM {$wpdb->prefix}commentmeta 
    
    
    WHERE {$wpdb->prefix}commentmeta.meta_key = 'rating'
    GROUP BY comment_id 
    ORDER BY avg_meta_value desc");
    foreach ($results as $result)
    {
        echo $result->comment_id.'<br>';
    }

the problem happens when I want to get a left join with the comments table:

even if the query is correct:

        <?php
        $results = $wpdb->get_results("
        SELECT comment_id, avg(meta_value) avg_meta_value
        FROM {$wpdb->prefix}commentmeta 
        
        LEFT JOIN $wpdb->prefix}comments
        ON {$wpdb->prefix}commentmetament_id = $wpdb->prefix}commentsment_ID
        
        
        WHERE {$wpdb->prefix}commentmeta.meta_key = 'rating'
        GROUP BY comment_id 
        ORDER BY avg_meta_value desc");
        
        foreach ($results as $result)
        {
            echo $result->comment_id.'<br>';
        }
        ?>

doing the print_r($ results); i get: Array( )

and I don't understand why, understanding this step is essential for me to make the next join with the post table, so that I can get all the values ​​out, why do I get array ()?

how can i fix it?

.... and

var_dump($result); return me `NULL`
Share Improve this question asked Oct 25, 2020 at 15:33 user14441695user14441695 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You have a mistake in your query

      $sql = "SELECT 
                    {$wpdb->prefix}commentmetament_id, avg({$wpdb->prefix}commentmeta.meta_value) avg_meta_value
                FROM 
                    {$wpdb->prefix}commentmeta
                LEFT JOIN 
                    {$wpdb->prefix}comments
                ON 
                    {$wpdb->prefix}commentmetament_id = {$wpdb->prefix}commentsment_ID
                WHERE 
                    {$wpdb->prefix}commentmeta.meta_key = 'rating'
                GROUP BY 
                    {$wpdb->prefix}commentmetament_id 
                ORDER BY 
                    avg_meta_value desc";
                    
        $results = $wpdb->get_results($sql);
        
        foreach ($results as $result)
        {
            echo $result->comment_id.'<br>';
        }
发布评论

评论列表(0)

  1. 暂无评论