带有3个表的SQL JOIN


SQL JOIN with 3 tables

我需要一些帮助才能从3个不同的表中获取数据。我可以自己研究,但这需要一段时间,我没有。

我需要的是:

Table Name     - columns needed
wp_bp_activity - item_id, user_id
wp_posts       -    ID  , -------, post_author, post_title
wp_users       - -------, user_id, -----------, ----------, display_name, user_email
WHERE type (in wp_bp_activity) = 'unit_complete' AND MONTH(date_recorded) (in wp_bp_activity) = $i

你能帮我用PHP PDO创建这个查询吗?

按要求编辑:

wp_bp_activity:  id     user_id     component   type    action  content     primary_link    item_id     secondary_item_id   date_recorded   hide_sitewide   mptt_left   mptt_right  is_spam
wp_posts:  id   post_author     post_date   post_date_gmt   post_content    post_title  post_excerpt    post_status     comment_status  ping_status     post_password   post_name   to_ping     pinged  post_modified   post_modified_gmt   post_content_filtered   post_parent     guid    menu_order  post_type   post_mime_type  comment_count
wp_users:  id   user_login  user_pass   user_nicename   user_email  user_url    user_registered     user_activation_key     user_status     display_name

我需要的是下面三张表中的信息,但作为一行。

wp_bp_activity表将根据PHP的另一部分将提供的月份WHERE date_recorded = $i向我提供item_iduser_iditem_id ID in wp_posts将提供我获得post_author - author of the unit coursepost_title - the name of the course所需的信息。之后,user_id将在wp_users中向我提供display_name - student's nameuser_email - student's email

最后,我将在item_iduser_id的基础上显示该单元在4月份(例如)完成unit_complete (MONTH(date_recorded))post_authorpost_titledisplay_nameuser_email

尝试以下SQL查询,并告诉我您得到了什么:

SELECT 
    activity.item_id,
    activity.user_id,
    posts.ID,
    posts.post_author,
    posts.post_title,
    users.display_name,
    users.user_email
FROM wp_bp_activity AS activity
JOIN wp_posts AS posts
    ON posts.ID = activity.item_id
JOIN wp_users AS users
    ON users.ID = activity.user_id
WHERE activity.type = 'unit_complete' 
    AND MONTH(activity.date_recorded) = $i