D
D
Dilik Pulatov2018-03-16 15:18:27
MySQL
Dilik Pulatov, 2018-03-16 15:18:27

Why does Yii2 handle MySQL queries incorrectly?

Hello!
I wanted to get the number of users by status
, so there is a user table and the status fields are 1 or 0
if I write a request in PhpMyAdmin, then everything works fine ....

SELECT SUM(IF(status = "1",1,0)) AS active,SUM(IF(status = "0",1,0)) AS inactive FROM `user`

and if I write in Yii2, then they display an error ..
the request itself
(new \yii\db\Query())->from('user')->select('SUM(IF(status = "1",1,0)) AS active, SUM(IF(status = "0",1,0)) AS inactive')->all()

a mistake
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS `status_active` FROM `user`' at line 1
The SQL being executed was: SELECT SUM(IF(status = "1", `1`, `0))` AS `status_active` FROM `user`

In my opinion, this is due to the fact that it does not correctly insert = "`" into the select,
how can I fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2018-03-16
@dilikpulatov

(new Query())->select([
    'SUM(IF(status = "1",1,0))' => 'active',
    'SUM(IF(status = "0",1,0))' => 'inactive'
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question