在$ wpdb查询中使用INNER JOIN

时间:2015-03-26 11:35:30

标签: mysql wpdb

我需要从两个由id链接的单独表中返回数据,但我不确定内部连接部分。

到目前为止我的代码:

<?php
    global $wpdb;
    $currentuser_id = get_current_user_id();

    $sql= "SELECT wp_awpcp_ads.ad_title, wp_awpcp_categories.category_name
                        FROM wp_awpcp_ads
                        INNER JOIN $wpdb-> wp_awpcp_categories
                        ON ($wpdb->wp_awpcp_ads.ad_category_id = $wpdb->wp_awpcp_categories.category_id) 
                        WHERE user_id ='".$currentuser_id."'";
                        $classifieds = $wpdb->get_results($sql);

    foreach ( $classifieds as $classified ) { ?>
        <h2><?php echo $classified->ad_title; ?></h2>
        <h3><?php echo $classified->category_name; ?></h3> 
    <?php }
?>

感谢您的任何建议

1 个答案:

答案 0 :(得分:1)

修改并尝试:

$sql= "SELECT wp_awpcp_ads.ad_title, wp_awpcp_categories.category_name
FROM wp_awpcp_ads
INNER JOIN wp_awpcp_categories
ON wp_awpcp_ads.ad_category_id = wp_awpcp_categories.category_id 
WHERE user_id ='".$currentuser_id."'";