Zend Query 中有很多很多的连接,希望只是轻微的调整

发布于 2024-08-23 04:35:32 字数 1768 浏览 7 评论 0原文

对所有这些代码表示歉意,无论如何,我将查询重新设计为 Zend 查询的工作方式,这就是我到目前为止所拥有的:

$db = Zend_Registry::get ( "db" );      
    $stmt = $db->query('
    SELECT recipe_pictures.picture_id, recipe_pictures.picture_filename, course.course_name, cuisines.name, recipes.id, recipes.Title, recipes.Method, recipes.author, recipes.SmallDesc, recipes.user_id, recipes.cuisine, recipes.course, recipes.Created_at, recipes.vegetarian, recipes.Acknowledgements, recipes.Time, recipes.Amount, recipes.view_count, recipes.recent_ips, guardian_writers.G_item, guardian_writers.G_type
    FROM recipes
    LEFT JOIN course ON recipes.course = course.course_id
    LEFT JOIN recipe_pictures ON recipes.id = recipe_pictures.recipe_id
    LEFT JOIN cuisines ON recipes.cuisine = cuisines.id
    LEFT JOIN guardian_writers ON recipes.author = guardian_writers.G_author
    WHERE recipes.id = ?', $id);
    $stmt->setFetchMode(Zend_Db::FETCH_ASSOC);
    $recipes = $stmt->fetchAll();

    return $recipes;

上面的代码有效,试图正确获取 Zend 版本,我的努力如下。

    $db = Zend_Registry::get ( "db" );      
    $select = $db->select()
                 ->from(array('r' => 'recipes'))
                 ->join(array('c' => 'course'),
                        'r.course = c.course_id')
                 ->join(array('rp' => 'recipe_pictures'),
                        'r.id = rp.recipe_id')
                 ->join(array('cui' => 'cuisines'),
                        'r.cuisine = cui.id')
                 ->join(array('gw' => 'guardian_writers'),
                        'r.author = gw.G_author')
                 ->where(' id = ? ', $id);

    $recipes = $db->fetchRow($select);

    return $recipes;

如果有人能发现错误,我将非常感激,谢谢

Apologies for all this code, anyhow Im re-working a query into the Zend query way of working, this is what I have so far:

$db = Zend_Registry::get ( "db" );      
    $stmt = $db->query('
    SELECT recipe_pictures.picture_id, recipe_pictures.picture_filename, course.course_name, cuisines.name, recipes.id, recipes.Title, recipes.Method, recipes.author, recipes.SmallDesc, recipes.user_id, recipes.cuisine, recipes.course, recipes.Created_at, recipes.vegetarian, recipes.Acknowledgements, recipes.Time, recipes.Amount, recipes.view_count, recipes.recent_ips, guardian_writers.G_item, guardian_writers.G_type
    FROM recipes
    LEFT JOIN course ON recipes.course = course.course_id
    LEFT JOIN recipe_pictures ON recipes.id = recipe_pictures.recipe_id
    LEFT JOIN cuisines ON recipes.cuisine = cuisines.id
    LEFT JOIN guardian_writers ON recipes.author = guardian_writers.G_author
    WHERE recipes.id = ?', $id);
    $stmt->setFetchMode(Zend_Db::FETCH_ASSOC);
    $recipes = $stmt->fetchAll();

    return $recipes;

That one above works, trying to get the Zend version properly, my effort is below.

    $db = Zend_Registry::get ( "db" );      
    $select = $db->select()
                 ->from(array('r' => 'recipes'))
                 ->join(array('c' => 'course'),
                        'r.course = c.course_id')
                 ->join(array('rp' => 'recipe_pictures'),
                        'r.id = rp.recipe_id')
                 ->join(array('cui' => 'cuisines'),
                        'r.cuisine = cui.id')
                 ->join(array('gw' => 'guardian_writers'),
                        'r.author = gw.G_author')
                 ->where(' id = ? ', $id);

    $recipes = $db->fetchRow($select);

    return $recipes;

If anyone can spot an error Id be very grateful, thanks

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

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

发布评论

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

评论(2

何必那么矫情 2024-08-30 04:35:32

使用 joinLeft 而不是 join 来生成左连接。

要从表中获取特定列,而不是全部 (*),请使用以下命令:

->from(array('r' => 'recipes'), array('id', 'title', 'method'))

->joinLeft(array('rp' => 'recipe_pictures'),
                    'r.id = rp.recipe_id',
                    array('picture_id', 'picture_filename')
                    )

要从表中不获取任何列,请传递一个空数组作为第三个参数。

Use joinLeft instead of join to produce left joins.

To fetch specific columns from a table, rather than all (*) use this:

->from(array('r' => 'recipes'), array('id', 'title', 'method'))

or

->joinLeft(array('rp' => 'recipe_pictures'),
                    'r.id = rp.recipe_id',
                    array('picture_id', 'picture_filename')
                    )

To fetch no columns from a table, pass an empty array as the third parameter.

人间不值得 2024-08-30 04:35:32

join 方法提供了 SQL INNER JOIN。如果您想获得LEFT JOIN,您应该使用joinLeft

The join method provides an sql INNER JOIN. If you want to get a LEFT JOIN you should use joinLeft.

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