Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii2查询错误_Php_Mysql_Yii2 - Fatal编程技术网

Php Yii2查询错误

Php Yii2查询错误,php,mysql,yii2,Php,Mysql,Yii2,我在yii2中有这个查询: SELECT COUNT(*) FROM `agencias` LEFT JOIN `responsables` `rsp` ON `agencias`.`responsable` = `rsp`.`responsable_id` LEFT JOIN `receptores` `rec` ON `agencias`.`receptor` = `rec`.`receptor_id` WHERE (`nombre

我在yii2中有这个查询:

SELECT COUNT(*) 
FROM `agencias` 
    LEFT JOIN `responsables` `rsp` 
        ON `agencias`.`responsable` = `rsp`.`responsable_id` 
    LEFT JOIN `receptores` `rec` 
        ON `agencias`.`receptor` = `rec`.`receptor_id` 
WHERE (`nombre` LIKE '%pru%') 
    AND (nombres LIKE "%%" or apellidos LIKE "%%")
但我得到消息“where子句中的'Column'nombre'不明确” 我知道如何修复此错误,为where条件设置别名,但不知道如何在yii2activequery中解决此问题

因此,问题是如何为where条件设置别名以获得如下查询:

SELECT COUNT(*) 
FROM `agencias` 
    LEFT JOIN `responsables` `rsp` 
        ON `agencias`.`responsable` = `rsp`.`responsable_id` 
    LEFT JOIN `receptores` `rec` 
        ON `agencias`.`receptor` = `rec`.`receptor_id` 
WHERE (`nombre` LIKE '%pru%') as alias1 
    AND (nombres LIKE "%%" or apellidos LIKE "%%") alias 2

您需要在
WHERE
子句中的
nombre
字段前面加上正确的表别名。假设您根据
responsables
中的
nombre
字段进行筛选,那么您的查询应该是:

SELECT COUNT(*) 
FROM `agencias` 
    LEFT JOIN `responsables` `rsp` 
        ON `agencias`.`responsable` = `rsp`.`responsable_id` 
    LEFT JOIN `receptores` `rec` 
        ON `agencias`.`receptor` = `rec`.`receptor_id` 
WHERE (`rsp`.`nombre` LIKE '%pru%') 
    AND (`nombres` LIKE "%%" or `apellidos` LIKE "%%")

您是否尝试在
nombre
之前添加
agencia.
?(考虑到此列确实在
agencias
表中)是的,并且它不会改变结果,因为它仍然会带来agencias的名称(nombre)和receptor的名称(nombre),因此问题就发生在这里。