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

<small id='U9S4i'></small><noframes id='U9S4i'>

        <bdo id='U9S4i'></bdo><ul id='U9S4i'></ul>
        <tfoot id='U9S4i'></tfoot><legend id='U9S4i'><style id='U9S4i'><dir id='U9S4i'><q id='U9S4i'></q></dir></style></legend>

      1. 如何在 Doctrine2 中使用 SQL 的 YEAR()、MONTH() 和 DAY()?

        时间:2023-06-01
      2. <small id='D9KCN'></small><noframes id='D9KCN'>

          <tbody id='D9KCN'></tbody>

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

                • <bdo id='D9KCN'></bdo><ul id='D9KCN'></ul>

                  本文介绍了如何在 Doctrine2 中使用 SQL 的 YEAR()、MONTH() 和 DAY()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在本机 SQL 中执行如下所示的查询:

                  I want to perform a query which would look like this in native SQL:

                  SELECT
                      AVG(t.column) AS average_value
                  FROM
                      table t
                  WHERE
                      YEAR(t.timestamp) = 2013 AND
                      MONTH(t.timestamp) = 09 AND
                      DAY(t.timestamp) = 16 AND
                      t.somethingelse LIKE 'somethingelse'
                  GROUP BY
                      t.somethingelse;
                  

                  如果我想像这样在 Doctrine 的查询构建器中实现这个:

                  If I am trying to implement this in Doctrine's query builder like this:

                  $qb = $this->getDoctrine()->createQueryBuilder();
                  $qb->select('e.column AS average_value')
                     ->from('MyBundle:MyEntity', 'e')
                     ->where('YEAR(e.timestamp) = 2013')
                     ->andWhere('MONTH(e.timestamp) = 09')
                     ->andWhere('DAY(e.timestamp) = 16')
                     ->andWhere('u.somethingelse LIKE somethingelse')
                     ->groupBy('somethingelse');
                  

                  我收到错误异常

                  [语法错误] 第 0 行,第 63 列:错误:预期已知函数,得到 'YEAR'

                  [Syntax Error] line 0, col 63: Error: Expected known function, got 'YEAR'

                  如何使用 Doctrines 查询构建器实现我的查询?

                  How can I implement my query with Doctrines query builder?

                  注意事项:

                  • 我了解 Doctrine 的原生 SQL.我已经尝试过这个,但它导致了我的生产数据库表和我的开发数据库表具有不同名称的问题.我想在数据库不可知的情况下工作,所以没有选择.
                  • 虽然我想工作与数据库无关:仅供参考,我使用的是 MySQL.
                  • 有一种方法可以扩展 Doctrine 以学习"YEAR() 等语句,例如如见此处.但我正在寻找一种避免包含第三方插件的方法.
                  • I know about Doctrine's Native SQL. I've tried this, but it leads to the problem that my productive and my development database tables have different names. I want to work database agnostic, so this is no option.
                  • Although I want to work db agnostic: FYI, I am using MySQL.
                  • There is way to extend Doctrine to "learn" the YEAR() etc. statements, e.g. as seen here. But I am looking for a way to avoid including third party plugins.

                  推荐答案

                  你可以添加Doctrine extension 所以如果您使用的是 Symfony,则可以通过添加此配置来使用 MySql YEARMONTH 语句:

                  You can add Doctrine extension so you can use the MySql YEAR and MONTH statement by adding this configuration if you're on Symfony:

                  doctrine:
                      orm:
                          dql:
                              string_functions:
                                  MONTH: DoctrineExtensions\Query\Mysql\Month
                                  YEAR: DoctrineExtensions\Query\Mysql\Year
                  

                  现在您可以在 DQL 或查询构建器中使用 MONTH 和 YEAR 语句.

                  now you can use the MONTH and YEAR statements in your DQL or querybuilder.

                  注意:扩展支持 MySQL、Oracle、PostgreSQL 和 SQLite.

                  Note: The extension supports MySQL, Oracle, PostgreSQL and SQLite.

                  这篇关于如何在 Doctrine2 中使用 SQL 的 YEAR()、MONTH() 和 DAY()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:WHERE 子句中的条件顺序是否会影响 MySQL 性能? 下一篇:在 MySQL 语句中使用 OR 到底有什么不同,带/不带括号?

                  相关文章

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

                  1. <tfoot id='vtNFk'></tfoot>

                      <small id='vtNFk'></small><noframes id='vtNFk'>

                      <legend id='vtNFk'><style id='vtNFk'><dir id='vtNFk'><q id='vtNFk'></q></dir></style></legend>
                      • <bdo id='vtNFk'></bdo><ul id='vtNFk'></ul>