左外连接如何在 Zend 框架上工作

发布于 2024-11-09 11:31:40 字数 964 浏览 7 评论 0原文

我有这个 SQL 查询:

SELECT pais FROM pais LEFT OUTER JOIN users_has_pais ON pais.id = users_has_pais.pais_id WHERE users_has_pais.users_id = 100

我正在尝试使用 Zend_Db_Table 中的 leftJoin 方法在模型中编写类似的内容,但我不知道我在做什么......我尝试过这样的事情:

$resultSetPais = Zend_Db_Table::getDefaultAdapter();
$some = $resultSetPais->select()
                      ->joinLeft( array ( 'users_has_pais' => 'users' ),
                          'pais.id = users_has_pais.pais_id', 'pais' );

但事实是我不知道如何使其工作,此代码仅返回适配器信息。

已解决:

        $instance = Zend_Db_Table_Abstract::getDefaultAdapter();
        $pais = $instance->select();
        $pais->from(array('p' => 'pais'), array('p.pais') )
             ->join( 'users_has_pais', 'p.id = users_has_pais.pais_id' )
             ->where( 'users_has_pais.users_id = ?', $row->id );
        $paisEntry = $instance->fetchCol($pais);

I have this SQL query:

SELECT pais FROM pais LEFT OUTER JOIN users_has_pais ON pais.id = users_has_pais.pais_id WHERE users_has_pais.users_id = 100

And I'm trying to write something similar within a model using the leftJoin method from Zend_Db_Table but I have no clue on what I'm doing.... I tried with something like this:

$resultSetPais = Zend_Db_Table::getDefaultAdapter();
$some = $resultSetPais->select()
                      ->joinLeft( array ( 'users_has_pais' => 'users' ),
                          'pais.id = users_has_pais.pais_id', 'pais' );

But truth is I have no idea how to make it work and this code just returns the adapter information.

SOLVED:

        $instance = Zend_Db_Table_Abstract::getDefaultAdapter();
        $pais = $instance->select();
        $pais->from(array('p' => 'pais'), array('p.pais') )
             ->join( 'users_has_pais', 'p.id = users_has_pais.pais_id' )
             ->where( 'users_has_pais.users_id = ?', $row->id );
        $paisEntry = $instance->fetchCol($pais);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁吢 2024-11-16 11:31:40

我按照@Jaitsu 的建议添加问题的答案。对于这种左连接:

SELECT pais FROM pais LEFT OUTER JOIN users_has_pais ON pais.id = users_has_pais.pais_id WHERE users_has_pais.users_id = 100

代码应该是这样的:

    $instance = Zend_Db_Table_Abstract::getDefaultAdapter();
    $pais = $instance->select();
    $pais->from(array('p' => 'pais'), array('p.pais') )
         ->join( 'users_has_pais', 'p.id = users_has_pais.pais_id' )
         ->where( 'users_has_pais.users_id = ?', $row->id );
    $paisEntry = $instance->fetchCol($pais);

I'm adding the answer to the question as suggested by @Jaitsu. For this kind of left join:

SELECT pais FROM pais LEFT OUTER JOIN users_has_pais ON pais.id = users_has_pais.pais_id WHERE users_has_pais.users_id = 100

The code should be something like:

    $instance = Zend_Db_Table_Abstract::getDefaultAdapter();
    $pais = $instance->select();
    $pais->from(array('p' => 'pais'), array('p.pais') )
         ->join( 'users_has_pais', 'p.id = users_has_pais.pais_id' )
         ->where( 'users_has_pais.users_id = ?', $row->id );
    $paisEntry = $instance->fetchCol($pais);
以可爱出名 2024-11-16 11:31:40

嘿这段代码生成一个 INNER JOIN sql 查询,而不是一个 OUTER JOIN - 这是不同的事情,对吧?那么,进行 OUTER JOIN 的正确方法应该是什么?

hey this code produces an INNER JOIN sql query, not an OUTER JOIN - it is different thing, right? so, what should be the correct way of doing OUTER JOIN?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文