ORDER BY不使用Symfony2中的原始SQL查询,但适用于MySQL数据库

时间:2015-01-06 23:04:40

标签: mysql symfony

我有一个查询,它返回我用于下拉日期选择器的日期列表,这是SQL查询。

SELECT `completion_date`
FROM `enviro_figures_upload`
GROUP BY YEAR( completion_date ) , MONTH( completion_date ) 
ORDER BY YEAR( completion_date ) ASC , MONTH( completion_date ) ASC

通过phpMyAdmin,查询以我需要的顺序返回我需要的结果:

2014-01-01
2014-02-01
2014-11-01
2014-12-01
2015-01-01
2015-02-01

在我的Symfony2应用程序中,这是我用来将这些数据提供给我的应用程序的代码:

    $sql4 = "SELECT `completion_date` FROM `enviro_figures_upload` GROUP BY YEAR(completion_date), MONTH(completion_date) ORDER BY YEAR(completion_date) ASC, MONTH(completion_date) ASC;";

    $activeDateSelect = $this->getDoctrine()->getManager()->getConnection()->prepare($sql4);
    $activeDateSelect->execute();

    $activeDate = $activeDateSelect->fetchAll();

但是,结果并没有按照正确的顺序排列。返回的结果如下:

2014-01-01
2015-01-01
2014-11-01
2014-12-01
2015-02-01

我无法理解为什么结果会像这样改变。我该怎么办?

修改 日期为VARCHAR而非DATE的字段类型。这是因为构建此功能的客户端无法更改Excel中用于创建CSV的日期格式。因此,日期以英国格式上传,然后我将日期重新格式化为当前格式。

这是fetchAll()

的var_dump
Array
(
    [0] => Array
        (
            [completion_date] => 
        )

    [1] => Array
        (
            [completion_date] => 2014-01-06
        )

    [2] => Array
        (
            [completion_date] => 2014-02-06
        )

    [3] => Array
        (
            [completion_date] => 2014-03-06
        )

    [4] => Array
        (
            [completion_date] => 2014-04-06
        )

    [5] => Array
        (
            [completion_date] => 2014-05-06
        )

    [6] => Array
        (
            [completion_date] => 2014-06-06
        )

    [7] => Array
        (
            [completion_date] => 2014-07-06
        )

    [8] => Array
        (
            [completion_date] => 2014-08-06
        )

    [9] => Array
        (
            [completion_date] => 2014-09-05
        )

    [10] => Array
        (
            [completion_date] => 2014-10-05
        )

    [11] => Array
        (
            [completion_date] => 2014-11-05
        )

    [12] => Array
        (
            [completion_date] => 2014-12-05
        )

    [13] => Array
        (
            [completion_date] => 2015-01-01
        )

    [14] => Array
        (
            [completion_date] => 2015-02-01
        )

    [15] => Array
        (
            [completion_date] => 2015-03-01
        )

)

编辑2
我通过Twig迭代结果:

{% for ad in actdate %}
     <option value="{{ad.completion_date|date('Y-m')}}">{{ad.completion_date|date('F Y')}}</option>
{% endfor %}

0 个答案:

没有答案