Yii2:查询未正确生成

问题描述 投票:0回答:1

我有这个问题:

$nginSumSubquery = NginAgentDetail::find()
->select(['CLIENT_ID', 'sum(ngin_agent_detail.VALUES) as nginSum','STR_TO_DATE(OPERATION_DATE, '."'%Y-%m-%d %H:%i'".') AS data_ngin'])
->groupBy(['ngin_agent_detail.CLIENT_ID', 'data_ngin']);

$sapSumSubquery = Sap::find()
         ->select(['Criado_por', 'sum(sap.Val_liq_moeda) as sapValue', 'STR_TO_DATE(Data_doc, '."'%d.%m.%Y %H:%i'".') AS Data_Sap'])
        ->groupBy(['sap.Criado_por', 'sap.Data_doc']);

$query->select(['etu_loja.ID', 'id_master', 'nginSum', 'sapValue', 'regiao', 'etu_loja.user', 'Atendedora', 'Loja', 'Data_Sap', 'data_ngin'])
->leftJoin(['nginAgentDetail' => $nginSumSubquery], 'nginAgentDetail.CLIENT_ID = etu_loja.ID')
->leftJoin(['sap' => $sapSumSubquery], 'sap.Criado_por = etu_loja.user')
->where(['data_ngin' => 'Data_Sap']);

并生成的sql查询:

SELECT 
    `etu_loja`.`ID`,
    `id_master`,
    `nginSum`,
    `sapValue`,
    `regiao`,
    `etu_loja`.`user`,
    `Atendedora`,
    `Loja`,
    `Data_Sap`,
    `data_ngin`
FROM
    `etu_loja`
        LEFT JOIN
    `master_loja` ON `etu_loja`.`id_master` = `master_loja`.`id`
        LEFT JOIN
    (SELECT 
        CLIENT_ID,
            SUM(ngin_agent_detail.VALUES) AS nginSum,
            STR_TO_DATE(OPERATION_DATE, '%Y-%m-%d') AS data_ngin
    FROM
        `ngin_agent_detail`
    GROUP BY `ngin_agent_detail`.`CLIENT_ID` , `data_ngin`) `nginAgentDetail` ON nginAgentDetail.CLIENT_ID = etu_loja.ID
        LEFT JOIN
    (SELECT 
        Criado_por,
            SUM(sap.Val_liq_moeda) AS sapValue,
            STR_TO_DATE(Data_doc, '%d.%m.%Y') AS Data_Sap
    FROM
        `sap`
    GROUP BY `sap`.`Criado_por` , `sap`.`Data_doc`) `sap` ON sap.Criado_por = etu_loja.user
WHERE
    `Data_Sap` = 'data_ngin'

为什么Yii在最后一个where子句的左侧添加反引号而不是右边的?

`WHERE
        `Data_Sap` = 'data_ngin'

如果我只是用mysql workbench中的反引号替换单引号,我的意思是,'data_ngin'到`data_ngin`查询有效。

如何使这个查询在Yii中工作?

php yii2
1个回答
1
投票

使用where()的方法hash format最适用于您想要选择列值的情况。 data_ngin被视为字符串而不是列的名称。

在您的情况下,您希望使用更类似于原始SQL语句的string format

->where('Data_Sap=data_ngin')

或者如果上述仍然不起作用

->where('`Data_Sap`=`data_ngin`')
© www.soinside.com 2019 - 2024. All rights reserved.