Delete with Select in Mysql

1

Good, I tried to make a query to delete all the tickets that do not have details, but it gives me an error of sql syntax.

DELETE FROM ticket t JOIN ticketdetail td ON t.id = td.idticket WHERE 
(SELECT count(*) FROM td JOIN t ON t.id = td.idticket) = 0;

The relationship of the tables is as follows: Ticket saves an id field that acts as a primary key and ticketdetail has an idticket field that would be its foreign key.

Thanks in advance.

    
asked by Jose Antonio Lopez Lopez 06.02.2018 в 10:07
source

4 answers

2

The simplest way to do what you want would be with LEFT JOIN , since idticket will be NULL in those rows where there are no matches.

DELETE t FROM ticket t
LEFT JOIN ticketdetail td ON t.id=td.idticket 
WHERE td.idticket IS NULL;

Let's see a proof of concept:

Code

SEE DEMO IN REXTESTER

CREATE TABLE IF NOT EXISTS ticket 
(
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ticket_name VARCHAR(25)
)ENGINE=INNODB;

INSERT INTO ticket (ticket_name)
    VALUES 
        ('Ticket 1'),
        ('Ticket 2'),
        ('Ticket 3')
;

CREATE TABLE IF NOT EXISTS ticket_detail 
(
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    ticket_id INT,
    ticket_fecha DATETIME,
    FOREIGN KEY fk_ticket(ticket_id) REFERENCES ticket(id)
    ON UPDATE CASCADE ON DELETE  CASCADE  
)ENGINE=INNODB;

INSERT INTO ticket_detail (ticket_id,ticket_fecha)
    VALUES 
    (1,'2016-10-12'),
    (1,'2016-10-13'),
    (2,'2016-10-14'),
    (1,'2016-10-15'),
    (1,'2016-10-16'),
    (2,'2017-10-17')
;

/*Antes del borrado*/
SELECT * FROM ticket;

DELETE t FROM ticket t
LEFT JOIN ticket_detail td ON t.id=td.ticket_id 
WHERE td.ticket_id IS NULL;

/*Después del borrado*/
SELECT * FROM ticket;

Result:

-Antes

id  ticket_name
1   Ticket 1
2   Ticket 2
3   Ticket 3

-Después

id  ticket_name
1   Ticket 1
2   Ticket 2
    
answered by 06.02.2018 / 10:50
source
1

If what you want is to delete the tickets that do not have details, that is to say that you want to delete the tickets that DO NOT EXIST in the TicketDetail table. To do this you simply have to make a subquery on ticketDetail: SELECT * FROM ticketdetail;

With what your DELETE would look like this:

DELETE FROM ticket WHERE id NOT EXISTS (SELECT * FROM ticketdetail);

This way you are eliminating all the tickets that DO NOT EXIST in the ticketDetails table.

    
answered by 06.02.2018 в 11:46
0

When using JOIN in the DELETE you have to specify the table from which you want the records to be deleted, try the following:

DELETE ticket 
FROM ticket 
JOIN ticketdetail td ON t.id = td.idticket 
WHERE (SELECT count(*) 
       FROM td 
       JOIN t ON t.id = td.idticket) = 0;
    
answered by 06.02.2018 в 10:40
0

Try this query:

DELETE FROM ticket where id not in
(SELECT t.id FROM ticket t join ticketdetail td on t.id = td.idticket)

When using the JOIN clause we are indicating that it is a relation of type INNER JOIN. If in the ticketdetail table said ticket does not have any details, then in our SELECT that ID will not be shown. That means that only IDs that have detail are being shown; therefore, those that are not shown are those that do not have detail.

    
answered by 06.02.2018 в 10:39