0

私が実行している次のクエリがあります。

$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 の 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))
        {

どんな助けでも大歓迎です

4

1 に答える 1

0

ソースが複数のテーブルにある結合されたデータ セットに対してクエリを実行する必要がある場合は、結合を行う必要があります。基本的に、post と postmeta の場合、2 つのテーブル間の接続を作成する必要があります。その接続は投稿 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...
于 2012-08-24T14:55:58.740 に答える