一次查询2个wordpress数据库表

时间:2012-08-24 13:54:45

标签: wordpress

我正在运行以下查询:

$query = "SELECT post_title, ID, post_date_gmt, post_author
        FROM wp_posts 
            WHERE post_type='course-manager' 
        AND post_status='publish' 
        AND SUBSTRING(post_date_gmt,1,4) = '$this->yearGet'
        AND SUBSTRING(post_date_gmt,6,2) = '$this->monthGet'
        AND post_author = '$userid'
        ORDER BY post_title"; 
        $query_result = mysql_query ($query);
        while ($info = mysql_fetch_array($query_result))
        {

目前这只是查询wordpress posts表。我想查询wordpress postmeta表中的数据,一个例子就是这样我在其中添加了“在postmeta表中的AND course_date:

 $query = "SELECT post_title, ID, post_date_gmt, post_author
        FROM wp_posts 
            WHERE post_type='course-manager' 
        AND post_status='publish' 
        AND SUBSTRING(post_date_gmt,1,4) = '$this->yearGet'
        AND SUBSTRING(post_date_gmt,6,2) = '$this->monthGet'
        AND post_author = '$userid'
            AND course_date = '$this->yearGet'
        ORDER BY post_title"; 
        $query_result = mysql_query ($query);
        while ($info = mysql_fetch_array($query_result))
        {

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

如果需要查询源来自多个表的组合数据集,则需要进行连接。基本上,您需要在post和postmeta的情况下在2个表之间建立连接,该连接将是post id。所以查询应该如下所示:

SELECT a.something, b.anotherthing
FROM wp_posts AS a JOIN wp_postmeta AS b
ON a.ID = b.post_id
//followed by all of your where clause...