Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 调整mysql数据库查询以筛选列值_Php_Mysql_Database - Fatal编程技术网

Php 调整mysql数据库查询以筛选列值

Php 调整mysql数据库查询以筛选列值,php,mysql,database,Php,Mysql,Database,对于熟悉数据库查询的人来说,这应该是一个简单的方法(我刚刚开始学习) 在我的多作者网站中,我使用以下功能获取用户发布的前两个类别: // Function to get the user's top two categories function GetTop2CategoryByUser($user_id, $taxonomy){ global $wpdb; $results=$wpdb->get_results( $wpdb->prepare(

对于熟悉数据库查询的人来说,这应该是一个简单的方法(我刚刚开始学习)

在我的多作者网站中,我使用以下功能获取用户发布的前两个类别:

// Function to get the user's top two categories
function GetTop2CategoryByUser($user_id, $taxonomy){
    global $wpdb;

    $results=$wpdb->get_results( $wpdb->prepare(
        "
       SELECT      tt.term_id as category, COUNT(p.ID) as count
       FROM        $wpdb->posts p

       JOIN        $wpdb->term_relationships tr
                   ON p.ID = tr.object_id

       JOIN        $wpdb->term_taxonomy tt
                   ON tt.term_taxonomy_id = tr.term_taxonomy_id
                   AND (tt.taxonomy = %s AND tt.term_taxonomy_id != 1)

       WHERE       p.post_author = %s
       GROUP BY    tt.term_id
       ORDER BY    count DESC LIMIT 2
    ",
        $taxonomy,
        $user_id
    ) );
    return $results;
}
对于我的问题:在
posts
表下,我有
post\u status
列,其值为
published
。我希望查询只获取
post\u status
设置为
published
的结果


我该怎么做?

只需添加到where中即可

and p.post_status = 'published'