Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting using LEFT JOIN

I want to delete from a table depending on data that exists on another table that references the first, however, I have the code that works and shows the value to be deleted when I run it as a SELECT stetement, however when I change that to DELETE it gives me errors, that I don't understand why they're there.

DELETE leadCustomer.* FROM coursework.leadCustomer LEFT JOIN coursework.flightBooking ON leadCustomer.customerID = flightBooking.customerID WHERE leadCustomer.customerID NOT IN ( SELECT customerID FROM (SELECT customerID, status FROM coursework.flightBooking) AS StatusCount where status IN  ('R','H') GROUP BY customerID ) AND leadCustomer.customerID = 8; 

Error:

ERROR:  syntax error at or near "leadCustomer" LINE 1: DELETE leadCustomer.* FROM coursework.leadCustomer LEFT JOIN...                ^  ********** Error **********  ERROR: syntax error at or near "leadCustomer" SQL state: 42601 Character: 8 

I am using postgres

like image 814
Matt Avatar asked Mar 20 '13 11:03

Matt


People also ask

How do you delete using LEFT join?

Delete left join table is used to delete rows from the left table that do not have matching records in the right table. Below is the syntax to of deleting rows with a left join that does not have matching rows in another table: Delete table1 from table1 LEFT JOIN table2 ON table1. col_name=table2.

Can we use LEFT join IN delete query?

We can also use the LEFT JOIN clause in the DELETE statement to delete rows in a table (left table) that does not have matching rows in another table (right table). Note that we only put T1 table after the DELETE keyword, not both T1 and T2 tables like we did with the INNER JOIN clause.

Can we delete data using join?

We use joins to combine data from multiple tables. To delete the same rows or related rows from the table at that time we use delete join. In this article let us see how to delete multiple data using DELETE using JOIN by using MSSQL as a server.


1 Answers

SAMPLE. DELETE RECORD IN TABLE 'A' IS THERE ARE NOT RECORD IN TABLE 'H'

DELETE A FROM ARTICULO_ALMACEN A LEFT JOIN HISTORICO_UNION H ON A.COD_ARTICULO = H.COD_ARTICULO AND A.COD_ALMACEN = H.COD_ARTICULO_ALMACEN AND A.TPROPIEDAD1 = H.PROPIEDAD1 AND A.TPROPIEDAD2 = H.PROPIEDAD2 AND A.TPROPIEDAD3 = H.PROPIEDAD3 WHERE H.COD_ARTICULO IS NULL 
like image 78
user3048858 Avatar answered Oct 11 '22 10:10

user3048858