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'