In the model I make a request to the database, I get all the data that I need and the date in the format 2016-10-02 09: 45: 25 + 03 . $ query = (new \ yii \ db \ Query ()) ->
select ('cd.date day,
c.host_id, cd.customer_id ')
-> from ('customer_download cd')
-> leftJoin ('customer c', '"c". "id" = "cd". "customer_id"')

But I need to bring this date to mind 2016-10-02 09:45:25 . In the query, I did it like this: to_char (cd.date, 'YYYY-MM-dd HH24: MI') as day

But when I insert this code in the first request to_char (cd.date, 'YYYY-MM-dd HH24: MI') day, I get an error syntax error at or near "AS" How to do it correctly?

    1 answer 1

    There is a solution:
    1. It is necessary to read the documentation.
    2. I use new \ yii \ db \ Query (), then I need to read
    3. It says: In addition to column names, you can also use SQL expressions. You must use an array format to use expressions that contain commas to prevent incorrect automatic escaping.

    Decision:

    $ query = (new \ yii \ db \ Query ()) ->
    select (["to_char (cd.date, 'YYYY-MM-dd HH24: MI') as day",
    "c.host_id", "cd.customer_id"])
    -> from ('customer_download cd')
    -> leftJoin ('customer c', '"c". "id" = "cd". "customer_id"')