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
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.
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. The other does not. The simple fix is to add a 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. If it returns more than one employee, a LIMIT 1
will pick an employee from that list at random. 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.
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. 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. 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. Hope this helps.
I believe you can resolve this by using an IN or Exists clause instead of using an equals. 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
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.
WHERE IDEMPLEADO = (
SELECT IDEMPLEADO FROM ENVIOS WHERE IDPRODUCTO = (
You can use 'IN' instead of '='
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:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.