LEFT JOIN仅从右表返回一行

时间:2015-11-27 13:45:53

标签: php mysql sql

我有以下查询;

"SELECT goals_challenges.*, 
    products_services.id as psid,
    products_services.url,
    products_services.feature_benefit
    FROM goals_challenges 
        LEFT JOIN products_services ON goals_challenges.id = products_services.goal_challenge_id
    WHERE persona_id = :persona_id"

这两个表都有一个' id'专栏,因此&ps;'别名。

但是,尽管有两条记录与products_services表中的goal_challenge_id匹配,但只有第一行作为结果集的一部分返回。

编辑:正确的数据

goals_challenges

id    persona_id    title            item_category         solution
173   14            Lead Gen         business challenge    advertising

products_services

id    goal_challenge_id     url                feature_benefit
1     173                   www.testurl.com    good for testing, mobile
2     173                   www.google.com     good for searching, well known

PHP代码,包括查询;

public function findByPersonaId($persona_id)
{
    try {
        $this->dblayer->beginTransaction();
        $stmt = $this->dblayer->prepare("SELECT goals_challenges.*, products_services.id as psid, products_services.url, products_services.feature_benefit from goals_challenges LEFT JOIN products_services ON goals_challenges.id = products_services.goal_challenge_id WHERE goals_challenges.persona_id = :persona_id");
        $stmt->bindParam(':persona_id', $persona_id);
        $stmt->execute();

        $this->dblayer->commit();

        $result_set = array();

        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $result_set[] = $this->mapObject($row); 
        }

        return $result_set;

    } catch (PDOException $e) {
        $this->dblayer->rollBack();
        echo $e->getMessage();
        exit;
    }
}

public function mapObject(array $row)
{
    $entry = new GoalChallenge();
    $entry->setId($row['id']);
    $entry->setPersonaId($row['persona_id']);
    $entry->setTitle($row['title']);
    $entry->setItemCategory($row['item_category']);
    $entry->setDescription($row['description']);
    $entry->setSolution($row['solution']);
    $entry->setProductService(new ProductService($row['psid'], $row['id'], $row['url'], explode(',', $row['feature_benefit'])));
    $entry->SetResearchChecklist($row['research_checklist']);
    $entry->setSubtopics($row['subtopics']);
    $entry->setKeywords($row['keywords']);
    $entry->setStatus($row['status']);

    return $entry;
}

我得到了什么

Array
(
    [id] => 173
    [persona_id] => 14
    [title] => Lead Gen
    [item_category] => Business Challenge    
    [solution] => Advertising
    [product_service] => 
    [research_checklist] => 0,0,0,0,0,0
    [psid] => 1
    [url] => www.google.com
    [feature_benefit] => good for testing, mobile
)

编辑:好的,所以我已经找到了我期待的结果,它只是不在同一个目标中的挑战对象 - 在PHP中显然是什么 - 任何想法?

我从goals_challenges表获取所有数据,但只获得products_services表中的第一行(id 1)。

我的查询有问题吗?我尝试过添加" GROUP BY goals_challenges.id"但它并没有改变结果。

2 个答案:

答案 0 :(得分:0)

您的查询看起来很好。 在ON条件下,列: - “goal_condition_id”是否具有主键。 检查两列是否都有主键。

由于 阿曼

答案 1 :(得分:0)

"SELECT gc.*, 
    ps.id as psid,
    ps.url,
    ps.feature_benefit
    FROM product_services ps 
        LEFT JOIN goals_challenges gc ON gc.id = ps.goal_challenge_id
    WHERE ps.persona_id = :persona_id"

由于您的goal_challenges表有1行,因此您尝试找到与product_services表匹配的任何内容。但只有一行,

  

选择意味着在这些条件下为我区分行和   限制不为我重复行。