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

sql - how to SELECT meta values that are not null?

programmeradmin2浏览0评论

I have a rating system for our products but there is an issue:

by the code, that will SELECT all comments in commentmeta table that have rating as meta_key. even those who do not have a rate (the user did comment but did not leave a rating for the product).
so I need to SELECT just those comments that the rating as meta_key is not NULL.
my current code:

$sql = $wpdb->prepare( "
    SELECT meta_value 
    FROM {$wpdb->prefix}commentmeta 
    INNER JOIN {$wpdb->prefix}comments ON {$wpdb->prefix}commentmetament_id = {$wpdb->prefix}commentsment_ID 
    WHERE comment_post_ID = %d AND meta_key = 'rating' ", get_the_ID() 
);
$results = $wpdb->get_results( $sql );

Thanks for helping me

I have a rating system for our products but there is an issue:

by the code, that will SELECT all comments in commentmeta table that have rating as meta_key. even those who do not have a rate (the user did comment but did not leave a rating for the product).
so I need to SELECT just those comments that the rating as meta_key is not NULL.
my current code:

$sql = $wpdb->prepare( "
    SELECT meta_value 
    FROM {$wpdb->prefix}commentmeta 
    INNER JOIN {$wpdb->prefix}comments ON {$wpdb->prefix}commentmetament_id = {$wpdb->prefix}commentsment_ID 
    WHERE comment_post_ID = %d AND meta_key = 'rating' ", get_the_ID() 
);
$results = $wpdb->get_results( $sql );

Thanks for helping me

Share Improve this question edited Aug 8, 2019 at 11:31 Sh.Dehnavi asked Aug 8, 2019 at 11:05 Sh.DehnaviSh.Dehnavi 1093 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I founded myself my searching:

$sql = $wpdb->prepare( "
    SELECT meta_value 
    FROM {$wpdb->prefix}commentmeta 
    INNER JOIN {$wpdb->prefix}comments ON {$wpdb->prefix}commentmetament_id = {$wpdb->prefix}commentsment_ID 
    WHERE comment_post_ID = %d AND meta_key = 'rating' AND meta_value IS NOT NULL AND meta_value <> '' ", get_the_ID() 
);
$results = $wpdb->get_results( $sql );
发布评论

评论列表(0)

  1. 暂无评论