简体   繁体   English

多个子查询,一个返回超过1行

[英]Multiple subqueries, one returns more than 1 row

 SELECT NOMBRE_E 'EMPLEADO'
 FROM EMPLEADOS
 WHERE IDEMPLEADO = (
     SELECT IDEMPLEADO FROM ENVIOS WHERE IDPRODUCTO = (
         SELECT B.IDPRODUCTO
         FROM ENVIOS B
         GROUP BY B.IDPRODUCTO
         ORDER BY COUNT(*) DESC
         LIMIT 1
     )
 )

ERROR: Subquery returns more than 1 row 错误:子查询返回的行数超过1

how to fix it? 如何解决?

A quick fix is to add another Limit 1. But it is hard to say if it is correct without more information. 快速解决方案是添加另一个限制1。但是,如果没有更多信息,很难说它是否正确。

SELECT NOMBRE_E 'EMPLEADO'
 FROM EMPLEADOS
 WHERE IDEMPLEADO = (
     SELECT IDEMPLEADO FROM ENVIOS WHERE IDPRODUCTO = (
         SELECT B.IDPRODUCTO
         FROM ENVIOS B
         GROUP BY B.IDPRODUCTO
         ORDER BY COUNT(*) DESC
         LIMIT 1
     )
     LIMIT 1
 )

You have two subqueries. 您有两个子查询。

     SELECT B.IDPRODUCTO
     FROM ENVIOS B
     GROUP BY B.IDPRODUCTO
     ORDER BY COUNT(*) DESC
     LIMIT 1

And

 SELECT IDEMPLEADO FROM ENVIOS WHERE IDPRODUCTO = (
     ...
 )

The first has a LIMIT 1 on it to guarantee it only returns one run. 第一个有一个LIMIT 1来保证它只返回一次运行。 The other does not. 另一个没有。 The simple fix is to add a LIMIT 1 . 简单的解决方法是添加一个LIMIT 1

 SELECT IDEMPLEADO FROM ENVIOS WHERE IDPRODUCTO = (
     ...
 ) LIMIT 1

However, you should first examine why this query is returning more than one row. 但是,您应该首先检查为什么此查询返回多个行。 It appears to be selecting an employee ID from the shipments table based on the product ID. 似乎正在根据产品ID从发货表中选择员工ID。 If it returns more than one employee, a LIMIT 1 will pick an employee from that list at random. 如果返回一名以上的雇员,则LIMIT 1将从该列表中随机选择一名雇员。 You probably don't want that. 您可能不想要那样。

If it's the case that it's returning the same employee ID multiple times, you can use DISTINCT to reduce it to just 1. 如果多次返回相同的员工ID,则可以使用DISTINCT将其减小为1。

 SELECT DISTINCT IDEMPLEADO FROM ENVIOS WHERE IDPRODUCTO = (
     ...
 )

If it's returning different employee IDs, you have to question whether this is the right query to be running. 如果返回的是不同的员工ID,则必须询问这是否是运行正确的查询。 For example, the first query uses a GROUP BY B.IDPRODUCTO ORDER BY COUNT(*) DESC LIMIT 1 to sort the list by how many times each B.IDPRODUCTO is seen and return the one that's seen the most. 例如,第一个查询使用GROUP BY B.IDPRODUCTO ORDER BY COUNT(*) DESC LIMIT 1来按每个B.IDPRODUCTO被查看的次数对列表进行排序,并返回被查看次数最多的列表。 That may also be appropriate for your other subquery. 这也可能适合您的其他子查询。

SELECT NOMBRE_E 'EMPLEADO' 
FROM EMPLEADOS 
WHERE IDEMPLEADO = (SELECT DISTINCT(IDEMPLEADO) 
                    FROM ENVIOS 
                    WHERE IDPRODUCTO = (SELECT B.IDPRODUCTO 
                                        FROM ENVIOS B 
                                        GROUP BY B.IDPRODUCTO ORDER BY COUNT(*) DESC LIMIT 1) );

In the outer subquery, maybe a distinct or MAX if it's numeric. 在外部子查询中,如果是数字,则可能是非重复或MAX。 Hope this helps. 希望这可以帮助。

I believe you can resolve this by using an IN or Exists clause instead of using an equals. 我相信您可以通过使用IN或Exists子句而不是使用等于来解决此问题。 This will allow the subquery to return multiple results 这将允许子查询返回多个结果

SELECT NOMBRE_E 'EMPLEADO'
FROM EMPLEADOS
where exists
(
        SELECT IDEMPLEADO
        FROM ENVIOS
        WHERE IDPRODUCTO = (
                SELECT B.IDPRODUCTO
                FROM ENVIOS B
                GROUP BY B.IDPRODUCTO
                ORDER BY COUNT(*) DESC LIMIT 1
                )
                and ENVIOS.IDEMPLEADO = Empleados.IDEMPLEADO
        );

or an IN clause 或IN子句

SELECT NOMBRE_E 'EMPLEADO'
FROM EMPLEADOS
WHERE IDEMPLEADO in (
        SELECT IDEMPLEADO
        FROM ENVIOS
        WHERE IDPRODUCTO = (
                SELECT B.IDPRODUCTO
                FROM ENVIOS B
                GROUP BY B.IDPRODUCTO
                ORDER BY COUNT(*) DESC LIMIT 1
                )
        )

Your first subquery is returning more than 1 row. 您的第一个子查询返回的行多于1条。

WHERE IDEMPLEADO = (
   SELECT IDEMPLEADO FROM ENVIOS WHERE IDPRODUCTO = (

You can use 'IN' instead of '=' 您可以使用“ IN”代替“ =”

WHERE IDEMPLEADO IN (
     SELECT IDEMPLEADO FROM ENVIOS WHERE IDPRODUCTO = (

However, it's better you check why you're getting multiple values (1-n), whether you want to use all or select limit 1 or maybe you have a new problem now:) 但是,最好检查为什么要获得多个值(1-n),是要使用全部还是选择限制1,或者现在遇到新问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM