In MySQL I get error 1242. For Delete and Insert Into

2

I have a Sales table that is very much related to many with a table Products For that reason, create the table DetailofProductsEnLaSale

CREATE TABLE IF NOT EXISTS  DetalleDeLosProductosEnLaVenta  (

   ID_Productos  INT NOT NULL,
   ID_Ventas  INT NOT NULL,
   PRIMARY KEY ( ID_Productos ,  ID_Ventas ),
   INDEX  fk_Productos_has_Ventas_Ventas1_idx  ( ID_Ventas  ),
   INDEX  fk_Productos_has_Ventas_Productos1_idx  ( ID_Productos  ),
   CONSTRAINT  fk_Productos_has_Ventas_Productos1 
   FOREIGN KEY ( ID_Productos )
   REFERENCES  Productos  ( ID_Productos )
   ON DELETE NO ACTION
   ON UPDATE NO ACTION,
   CONSTRAINT  fk_Productos_has_Ventas_Ventas1 
   FOREIGN KEY ( ID_Ventas )
   REFERENCES  Ventas ( ID_Ventas )
   ON DELETE NO ACTION
   ON UPDATE NO ACTION)
;

To erase sales made on a date, I first try to delete the relationship in the table DetailofProductsEnLaSale

DELETE DetalleDeLosProductosEnLaVenta.* 
FROM DetalleDeLosProductosEnLaVenta
WHERE 
DetalleDeLosProductosEnLaVenta.ID_Ventas  = 
(select Ventas.ID_Ventas from Ventas
        where Ventas.Fecha = '2017/01/21' );

It happens that by that date there are more than two sales and I get the error

  

ERROR 1242 Subquery returns more than 1 row

I checked many pages like: Error 1242 Mysql subquery

Where you tell me to use limit 1, that is, limit to only deleting one.

I appreciate your cooperation ...

    
asked by Christian Ponisio 13.09.2017 в 19:51
source

3 answers

0

Seeing in your CREATE TABLE that the detalles table is related to the ventas table by IdVenta , you can delete using JOIN.

Notice that it would be almost the same query you would use for SELECT :

DELETE dp 
FROM Detalles_Productos_20170913 dp
INNER JOIN Ventas_20170913 v
ON dp.ID_Ventas = v.ID_Ventas 
WHERE v.Fecha = '2017/01/21' ; 

Código: DEMO EN REXTESTER

CREATE TABLE IF NOT EXISTS  Ventas_20170913  (
   ID_Ventas  INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Fecha DATETIME 
)
;

INSERT INTO Ventas_20170913 (Fecha)
    VALUES
    ('2017/01/21'),
    ('2017/01/21'),
    ('2017/01/22'),
    ('2017/01/22'),
    ('2017/01/22')
    ;

CREATE TABLE IF NOT EXISTS  Productos_20170913  (

   ID_Productos  INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Nombre VARCHAR (70) 
)
;

INSERT INTO Productos_20170913 (Nombre)
    VALUES
    ('Producto 1'),
    ('Producto 2'),
    ('Producto 3'),
    ('Producto 4'),
    ('Producto 5')

    ;

CREATE TABLE IF NOT EXISTS  Detalles_Productos_20170913  (
   ID_Productos  INT NOT NULL,
   ID_Ventas  INT NOT NULL,
   PRIMARY KEY ( ID_Productos ,  ID_Ventas ),
   INDEX  fk_Productos_has_Ventas_Ventas1_idx  ( ID_Ventas  ),
   INDEX  fk_Productos_has_Ventas_Productos1_idx  ( ID_Productos  ),
   CONSTRAINT  fk_Productos_has_Ventas_Productos1 
   FOREIGN KEY ( ID_Productos )
   REFERENCES  Productos_20170913  ( ID_Productos )
   ON DELETE NO ACTION
   ON UPDATE NO ACTION,
   CONSTRAINT  fk_Productos_has_Ventas_Ventas1 
   FOREIGN KEY ( ID_Ventas )
   REFERENCES  Ventas_20170913 ( ID_Ventas )
   ON DELETE NO ACTION
   ON UPDATE NO ACTION)
;

INSERT INTO Detalles_Productos_20170913 (ID_Productos,ID_Ventas)
    VALUES
    (1,1),
    (1,2),
    (3,3),
    (2,4),
    (4,5)
;

-- Antes de borrar 

SELECT * FROM Detalles_Productos_20170913 dp
INNER JOIN Ventas_20170913 v
ON dp.ID_Ventas = v.ID_Ventas ;

-- Borrar

DELETE dp 
FROM Detalles_Productos_20170913 dp
INNER JOIN Ventas_20170913 v
ON dp.ID_Ventas = v.ID_Ventas 
WHERE v.Fecha = '2017/01/21' ; 

-- Después de borrar

SELECT * FROM Detalles_Productos_20170913 dp
INNER JOIN Ventas_20170913 v
ON dp.ID_Ventas = v.ID_Ventas ;

Resultado:

-- Antes de borrar

ID_Productos    ID_Ventas   ID_Ventas   Fecha
1               1           1           21.01.2017 00:00:00
1               2           2           21.01.2017 00:00:00
3               3           3           22.01.2017 00:00:00
2               4           4           22.01.2017 00:00:00
4               5           5           22.01.2017 00:00:00


--Después de borrar

ID_Productos    ID_Ventas   ID_Ventas   Fecha
3               3           3           22.01.2017 00:00:00
2               4           4           22.01.2017 00:00:00
4               5           5           22.01.2017 00:00:00
    
answered by 13.09.2017 / 21:30
source
1

If you wanted to delete all the sales of a particular date, it would be enough for you to change the where of your query to this:

DetalleDeLosProductosEnLaVenta.ID_Ventas  in
(select Ventas.ID_Ventas from Ventas
    where Ventas.Fecha = '2017/01/21' );

Why? because the clause in is used to match a column to a list of data, which is what you want in this case.

    
answered by 13.09.2017 в 20:05
0

Guys, thank you for your answers, all are valid. My problem was that the WorkBench gave me errors and using console I see that all the solutions are right. I must update my WorkBench. Thanks

    
answered by 16.09.2017 в 18:49