Yii2 request error

I have this request in 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 "%%")

      

But I get the message "Column" nombre "where the sentence is ambiguous" I know how to fix this error by setting an alias for the conditions, but I don't know how it works in yii2 activequery.

So the question is, how can I set aliases for conditions to get a request like this:

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

      

+3


source to share


1 answer


You need to prefix the field nombre

in the sentence WHERE

with the correct table alias. Suppose you are filtering based on field nombre

in responsables

, then your query should be:



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 "%%")

      

+2


source







All Articles