Problem with subquery

0

I have the following sentence that helps me to update the fields of a detail_dulce table that collects only numerical values

update producto
inner join detalle_dulce on detalle_dulce.idp = producto.idp
inner join existencia on producto.idp = existencia.idp 
inner join codigo_barras on detalle_dulce.idp = codigo_barras.idp
set detalle_dulce.categoria = (select idc from categoria where nombre = 'dulce'), detalle_dulce.subcategoria = (select idsub from subcategoria where nombre = 'chocolate'), detalle_dulce.proveedor = (select idpr from proveedor where proveedor = 'berny'), existencia.cantidad = existencia.cantidad + 30
where producto.idp = 1551;

The problem is that when I run the error appears

Error Code: 1242. Subquery returns more than 1 row
    
asked by Hugo Costilla 23.07.2018 в 18:57
source

2 answers

1

What happens is that one of your subqueries, if you run it separately is very sure that it returns more than 1 record, so when using the comparator "=" the error jumps.

If you are aware that the subquery returns more than 1 value, use "IN", so that it updates all the records that are within that result set.

set detalle_dulce.categoria IN (select idc from categoria where nombre = 'dulce')

If you do not expect more than two records, use a limiter to get one (in sql server you can use Top (1), in mysql, I'm not sure). Ex:

set detalle_dulce.categoria = (select top(1) idc from categoria where nombre = 'dulce')

Greetings.

    
answered by 23.07.2018 / 19:02
source
0

you must identify which subquery is bringing you more than one data, for this in mysql I would do the following:

 update producto
 inner join detalle_dulce on detalle_dulce.idp = producto.idp
 inner join existencia on producto.idp = existencia.idp 
 inner join codigo_barras on detalle_dulce.idp = codigo_barras.idp
 set detalle_dulce.categoria = (select idc from categoria where nombre = 
 'dulce' limit 1), detalle_dulce.subcategoria = (select idsub from subcategoria 
 where nombre = 'chocolate' limit 1), detalle_dulce.proveedor = (select idpr from 
 proveedor where proveedor = 'berny' limit 1), existencia.cantidad = 
 existencia.cantidad + 30
 where producto.idp = 1551;

If this updates you, the most likely to happen, it is because one of the 3 subquery that adds "limit 1" is bringing you more than 1 data.

to identify what it is, you must leave only 1 of the 3 query with "limit 1" when the update happens, it is because that query is with problems, and it is in it that you must work.

    
answered by 23.07.2018 в 19:22