• <i id='dYWuH'><tr id='dYWuH'><dt id='dYWuH'><q id='dYWuH'><span id='dYWuH'><b id='dYWuH'><form id='dYWuH'><ins id='dYWuH'></ins><ul id='dYWuH'></ul><sub id='dYWuH'></sub></form><legend id='dYWuH'></legend><bdo id='dYWuH'><pre id='dYWuH'><center id='dYWuH'></center></pre></bdo></b><th id='dYWuH'></th></span></q></dt></tr></i><div id='dYWuH'><tfoot id='dYWuH'></tfoot><dl id='dYWuH'><fieldset id='dYWuH'></fieldset></dl></div>

  • <small id='dYWuH'></small><noframes id='dYWuH'>

  • <legend id='dYWuH'><style id='dYWuH'><dir id='dYWuH'><q id='dYWuH'></q></dir></style></legend>
    <tfoot id='dYWuH'></tfoot>

      <bdo id='dYWuH'></bdo><ul id='dYWuH'></ul>

      1. Yii2 - 在多个条件下左连接

        Yii2 - left join on multiple condition(Yii2 - 在多个条件下左连接)

      2. <i id='WpShX'><tr id='WpShX'><dt id='WpShX'><q id='WpShX'><span id='WpShX'><b id='WpShX'><form id='WpShX'><ins id='WpShX'></ins><ul id='WpShX'></ul><sub id='WpShX'></sub></form><legend id='WpShX'></legend><bdo id='WpShX'><pre id='WpShX'><center id='WpShX'></center></pre></bdo></b><th id='WpShX'></th></span></q></dt></tr></i><div id='WpShX'><tfoot id='WpShX'></tfoot><dl id='WpShX'><fieldset id='WpShX'></fieldset></dl></div>

            • <small id='WpShX'></small><noframes id='WpShX'>

              <legend id='WpShX'><style id='WpShX'><dir id='WpShX'><q id='WpShX'></q></dir></style></legend>
                  <tbody id='WpShX'></tbody>
                  <bdo id='WpShX'></bdo><ul id='WpShX'></ul>

                  <tfoot id='WpShX'></tfoot>
                  本文介绍了Yii2 - 在多个条件下左连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有三个表具有以下关系,

                  I have three tables with the following relations,

                    ------- 1        0..* ------------
                   |Product|-------------|Availability|
                    -------               ------------
                      1 |
                        |
                      1 |
                    --------
                   |MetaData|
                    --------
                  

                  我的原始 sql 看起来像这样

                  my raw sql looks like this

                  SELECT p.ID FROM product p 
                  LEFT JOIN availability a ON a.productID=p.ID 
                            AND a.start>=DATE_ADD(DATE(now()), INTERVAL 7 DAY)
                  LEFT JOIN meta_data m ON m.ID=p.meta_dataID
                  WHERE a.ID IS NULL
                  AND m.published_state=1;
                  

                  也就是说,找到每个 ProductMetaData.published_state 等于 1 而没有 Availability 这样的Availability.startnow() 开始超过 7 天.

                  That is, find each Product with a MetaData.published_state equal to 1 and with no Availability such that Availability.start more than 7 days from now().

                  我正在尝试使用 ActiveRecord 方法来完成相同的操作,使用类似以下内容,

                  I'm trying to accomplish the same using ActiveRecord methods, using something like the following,

                  $products = Product::find()
                              ->joinWith('metaData')
                              ->joinWith('availability')
                              ->onCondition(['>=', 'availability.start', strtotime('+7 days')])
                              ->where(['is', 'availability.ID', NULL])
                              ->andWhere(['=', 'meta_data.published_state', 1])
                              ->all();
                  

                  然而,这没有返回任何结果.使用 Connection::createCommand() 运行原始 sql 会返回我期望的行,因此数据没有问题.

                  however, this is returning no results. Using Connection::createCommand() to run the raw sql returns the rows I'd expect so there is no issue with the data.

                  我怀疑问题是由 join 条件和 where 条件相互渗透"引起的;join 和 where 都应用于 join 或 where 而不是分开.

                  I suspect the issue is being caused by the join conditions and the where conditions 'bleeding' into each other; both join and where being applied to either the joining or the where rather than separately.

                  如何输出正在运行的实际 sql 查询?这是从控制台控制器调用的操作.

                  How can I output the actual sql query being run? this is in an action being called from a console controller.

                  如何更改我的代码以返回所需的Products?

                  How can I alter my code to return the desired Products?

                  推荐答案

                  我相信这是更好的解决方案.不要使用像 leftJoin 这样的原始查询,你应该用 andOnCondition 补充你的 joinWith 关系(这会在你的 join 语句中添加需要的 where 条件).

                  I believe this one is better solution. Instead of using Raw queries like leftJoin you should complement your joinWith relations with andOnCondition (which adds needed where conditions into your join statement).

                  $products = Product::find()
                      ->joinWith(['metaData' => function (ActiveQuery $query) {
                          return $query
                              ->andWhere(['=', 'meta_data.published_state', 1]);
                      }])
                      ->joinWith(['availability' => function (ActiveQuery $query) {
                          return $query
                              ->andOnCondition(['>=', 'availability.start', strtotime('+7 days')])
                              ->andWhere(['IS', 'availability.ID', NULL]);
                      }])
                      ->all();
                  

                  此外,当您在关系中编写 where 子句时,它看起来更干净.它的工作原理和在外面写一样(如果我没记错的话),但是在重构查询时,您可以轻松删除整个关系,而不会忘记外面的关系条件.

                  In addition it looks cleaner when you write where clauses inside relations. It works the same as writing it outside (if I'm not wrong), but when refactoring your query, you can easily delete the whole relation without forgetting relation conditions outside.

                  这篇关于Yii2 - 在多个条件下左连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <tbody id='oM7jD'></tbody>
                  <tfoot id='oM7jD'></tfoot>
                    <bdo id='oM7jD'></bdo><ul id='oM7jD'></ul>

                    1. <i id='oM7jD'><tr id='oM7jD'><dt id='oM7jD'><q id='oM7jD'><span id='oM7jD'><b id='oM7jD'><form id='oM7jD'><ins id='oM7jD'></ins><ul id='oM7jD'></ul><sub id='oM7jD'></sub></form><legend id='oM7jD'></legend><bdo id='oM7jD'><pre id='oM7jD'><center id='oM7jD'></center></pre></bdo></b><th id='oM7jD'></th></span></q></dt></tr></i><div id='oM7jD'><tfoot id='oM7jD'></tfoot><dl id='oM7jD'><fieldset id='oM7jD'></fieldset></dl></div>
                          • <small id='oM7jD'></small><noframes id='oM7jD'>

                            <legend id='oM7jD'><style id='oM7jD'><dir id='oM7jD'><q id='oM7jD'></q></dir></style></legend>