如何选择不为空的元值?

时间:2019-08-08 作者:Sh.Dehnavi

我对我们的产品有一个评级系统,但存在一个问题:

根据代码,将选择中的所有注释commentmeta 具有ratingmeta_key. 甚至那些没有评分的人(用户有评论,但没有给产品留下评分)
so I need to SELECT just those comments that the rating as meta_key is not NULL.
我的当前代码:

$sql = $wpdb->prepare( "
    SELECT meta_value 
    FROM {$wpdb->prefix}commentmeta 
    INNER JOIN {$wpdb->prefix}comments ON {$wpdb->prefix}commentmeta.comment_id = {$wpdb->prefix}comments.comment_ID 
    WHERE comment_post_ID = %d AND meta_key = \'rating\' ", get_the_ID() 
);
$results = $wpdb->get_results( $sql );
谢谢你帮我

1 个回复
最合适的回答,由SO网友:Sh.Dehnavi 整理而成

我发现自己在寻找:

$sql = $wpdb->prepare( "
    SELECT meta_value 
    FROM {$wpdb->prefix}commentmeta 
    INNER JOIN {$wpdb->prefix}comments ON {$wpdb->prefix}commentmeta.comment_id = {$wpdb->prefix}comments.comment_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 );