La subconsulta devuelve un error de más de una fila, las respuestas aquí no funcionan

Resuelto Ryan Speciale asked hace 10 meses • 1 respuestas

Me encuentro con un error de subconsulta, pero las respuestas aquí dicen que use IN () o Join. Intenté usar ambos y sigo recibiendo el error. Quiero que obtenga el recuento de varios usuarios y no cuente los duplicados según el property_id. Si alguien puede ayudarme aquí, pondré los ID de los miembros del equipo en una tabla temporal con una columna, guardaré un SELECT * en esa tabla en una variable y lo usaré en la expresión IN. Aquí está mi código. El error que recibo es "la subconsulta devuelve más de 1 fila". p_membersListes una matriz JSON de ID de usuario

DROP TEMPORARY TABLE IF EXISTS temp;
CREATE TEMPORARY TABLE IF NOT EXISTS temp(
    memberId INT(10)
); 
WHILE `_index` < `length` DO
    INSERT INTO temp VALUES(JSON_UNQUOTE(JSON_EXTRACT(`p_membersList`,CONCAT('$[',`_index`,']'))));
    SET `_index` = `_index` + 1;
END WHILE;
SET @result:=(SELECT * FROM temp);
-- Total Count According To Filter
SELECT @result AS 'result';
SELECT count(distinct property_id) AS 'totalCount'
FROM `property` AS `property`
INNER JOIN
    `user_additional_data` AS `uad` ON `uad`.`user_id` = `property`.`user_id`
WHERE `property`.`user_id` IN (@result)
    OR `property`.`property_id` IN (
        SELECT `pm`.`property_id` AS `propertyId`
        FROM `property_member` AS `pm`
        INNER JOIN
            `property` AS `p1` ON `p1`.`user_id` = `pm`.`user_id`
        WHERE `pm`.`user_id`  IN (@result)
        GROUP BY `pm`.`user_id`
        UNION ALL
        SELECT
            -- `pm`.`property_id` AS `propertyId`
            `p2`.`property_id` AS `propertyId`
        FROM `property_member` AS `pm`
        INNER JOIN
            `property` AS `p2` ON `p2`.`user_id` = `pm`.`user_id`
        WHERE `pm`.`created_by` IN (@result)
        GROUP BY `pm`.`user_id`
        UNION ALL
        SELECT `pm`.`property_id` AS `propertyId`
        FROM `property_member` AS `pm`
        WHERE `pm`.`created_by` IN (@result)
           OR `pm`.`user_id` IN (@result)
        GROUP BY `pm`.`user_id`)
  -- AND `property`.`is_draft` = '0'AND `property`.`is_active` = '1'AND `property`.`is_deleted` = '0';
  AND IF(p_filter1 = '1', (`property`.`is_active` = '1' AND `property`.`is_draft` = '0'
    AND `property`.`is_deleted` = '0'
    ), IF(p_filter1 = '2', (`property`.`is_draft` = '1' AND `property`.`is_active` = '0'
    AND `property`.`is_deleted` = '0'
    ), IF(p_filter1 = '3', (`property`.`is_active` = '0' AND `property`.`is_draft` = '0'
    AND `property`.`is_deleted` = '0'
    ), IF(p_filter1 = '4', ((`property`.`is_active` = '1' OR `property`.`is_draft` = '1')
    AND `property`.`is_deleted` = '0'
    ), ""))))
  AND (
            `uad`.`is_primary_owner` = '0'
        OR
            (`uad`.`is_primary_owner` <> '0' AND created_by IN (@result))
        OR
            (`uad`.`is_primary_owner` <> '0' AND created_by = `uad`.`primary_owner`)
        OR
            (`uad`.`is_primary_owner` = '1' AND `uad`.is_enterprise_code = '1')
    );
Ryan Speciale avatar Feb 16 '24 06:02 Ryan Speciale
Aceptado

No se puede establecer una variable con múltiples valores, por lo que

SET @result:=(SELECT * FROM temp);

no funcionará. Simplemente únete a la tempmesa en lugar de usar IN (@result).

        SELECT `pm`.`property_id` AS `propertyId`
        FROM `property_member` AS `pm`
        INNER JOIN
            `property` AS `p1` ON `p1`.`user_id` = `pm`.`user_id`
        INNER JOIN temp ON pm.user_id = temp.memberId
        GROUP BY `pm`.`user_id`
Barmar avatar Feb 15 '2024 23:02 Barmar