Delete multiple join - mysql

In my other question I asked how to select a column from multiple tables with inner joins. My new question is: how to delete these results?
SELECT
product_image.image
FROM product
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type"

If you want to delete only from products then the below should do the job
delete p from product p
INNER JOIN ixml_prd_map ipm ON p.sku = ipm.id_oc_prd
INNER JOIN product_image pi ON pi.product_id = p.product_id
WHERE p.model = "xy-type"
But if you need to delete from all the tables matching the joining condition then use
delete p,ipm,pi from product p
INNER JOIN ixml_prd_map ipm ON p.sku = ipm.id_oc_prd
INNER JOIN product_image pi ON pi.product_id = p.product_id
WHERE p.model = "xy-type"

You can use this query
DELETE FROM product_image WHERE product_image_id IN (SELECT
product_image.image
FROM product
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type")
EDIT : From the manual
Currently, you cannot delete from a table and select from the same
table in a subquery.
If you want to modify the same query you can execute it by creating a temporary table (here its resultset)
DELETE FROM product_image WHERE product_image_id IN ( SELECT resultset.product_image_id FROM (SELECT
product_image.product_image_id
FROM product
INNER JOIN ixml_prd_map ON product.sku = ixml_prd_map.id_oc_prd
INNER JOIN product_image ON product_image.product_id = product.product_id
WHERE product.model = "xy-type") AS resultset )
OR you can use the USING like this in the example from the MySQL Manual,
13.2.2 DELETE Syntax. I haven't used the USING, but you can definetely check out.
DELETE FROM t1, t2 USING t1
INNER JOIN t2
INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
Also this SO post will help too MySQL Error 1093 - Can't specify target table for update in FROM clause

Related

Using SUM function in an update query with inner joins

Getting an error when running this
UPDATE FlightBooking
INNER JOIN Passenger ON Passenger.FlightBookingId=FlightBooking.FlightBookingId
INNER JOIN AirplaneSeat ON AirplaneSeat.AirplaneSeatId = Passenger.SeatId
INNER JOIN Section ON AirplaneSeat.SectionId = Section.SectionId
INNER JOIN ExtraCost ON ExtraCost.FlightBookingId=FlightBooking.FlightBookingId
INNER JOIN Luggage ON Luggage.LuggageId = ExtraCost.LuggageId
INNER JOIN SportsEquipment ON ExtraCost.SportsEquipmentId=SportsEquipment.SportsEquipmentId
INNER JOIN Insurance ON ExtraCost.InsuranceId = Insurance.InsuranceId
INNER JOIN CarHirePrice ON CarHirePrice.CarHirePriceId= ExtraCost.CarHirePriceId
INNER JOIN Route ON FlightBooking.RouteId = Route.RouteId
SET FlightBooking.TotalCost = (SUM(Section.PriceInflux+Route.RoutePrice+Luggage.Price+SportsEquipment.SportsEquipmentPrice+Insurance.Price+CarHirePrice.TotalPrice))
WHERE FlightBooking.FlightBookingId=1;
When I have it formed as a Select query it returns the correct value so all the tables are fine. I'm assuming my syntax is wrong.
Any help would be appreciated.
You should use the following syntax when using an aggregate in an update statement.
UPDATE t1
SET t1.field = t2.field2Sum
FROM table1 t1
INNER JOIN (select field3, sum(field2) as field2Sum
from table2
group by field3) as t2
on t2.field3 = t1.field3
See Below, I only scripted a few tables but you can see how to do the rest.
UPDATE FlightBooking set TotalCost = (ExtraCost.SumExtra + SumLuggage) From FlightBooking
INNER JOIN Passenger ON Passenger.FlightBookingId=FlightBooking.FlightBookingId
INNER JOIN AirplaneSeat ON AirplaneSeat.AirplaneSeatId = Passenger.SeatId
INNER JOIN Section ON AirplaneSeat.SectionId = Section.SectionId
INNER JOIN (Select FlightBookingId, sum(ExtraCost) as SumExtra from Extracost Group by FlightBookingId) as ExtraCost
ON ExtraCost.FlightBookingId=FlightBooking.FlightBookingId
INNER JOIN (Select FlightBookingId, sum(Price) as SumLuggage from Luggage Group by FlightBookingId) as Luggage
ON Luggage.FlightBookingId=FlightBooking.FlightBookingId
Are you sure that you have multiple records that match a single booking id?
If not, you can dispense with the sum():
SET FlightBooking.TotalCost = (Section.PriceInflux+Route.RoutePrice+Luggage.Price+SportsEquipment.SportsEquipmentPrice+Insurance.Price+CarHirePrice.TotalPrice)
If not, you'll need to pre-aggregate the tables that could generate multiple rows. In fact, you need to do this anyway to get a valid result (Cartesian products generated by joins will throw off the over sum).
many different possibilities for using aggregate funtion with update and inner join
UPDATE flight
SET flight.TotalCost = t.sumPrice
FROM FlightBooking AS flight
INNER JOIN
(
SELECT SUM(Section.PriceInflux+Route.RoutePrice+Luggage.Price+SportsEquipment.SportsEquipmentPrice+Insurance.Price+CarHirePrice.TotalPrice)) as sumPrice
FROM Passenger ON Passenger.FlightBookingId=flight.FlightBookingId
INNER JOIN AirplaneSeat ON AirplaneSeat.AirplaneSeatId = Passenger.SeatId
INNER JOIN Section ON AirplaneSeat.SectionId = Section.SectionId
INNER JOIN ExtraCost ON ExtraCost.FlightBookingId=flight.FlightBookingId
INNER JOIN Luggage ON Luggage.LuggageId = ExtraCost.LuggageId
INNER JOIN SportsEquipment ON ExtraCost.SportsEquipmentId=SportsEquipment.SportsEquipmentId
INNER JOIN Insurance ON ExtraCost.InsuranceId = Insurance.InsuranceId
INNER JOIN CarHirePrice ON CarHirePrice.CarHirePriceId= ExtraCost.CarHirePriceId
INNER JOIN Route ON flight.RouteId = Route.RouteId
) t
WHERE flight.bookingID = 1
also have a look to this examples
PC #PauloSantos and #OMGPonieshttps://stackoverflow.com/a/2009981/4426282

MySQL - #1066 - Not unique table/alias: 'components' with multiple inner joins

I have this query and I am getting error #1066 - Not unique table/alias: 'components'. What seems to be the issue?
SELECT COUNT(*) FROM `products`, `components`, `tradeNames`
INNER JOIN `componentsMap` ON componentsMap.product_id = product.id
INNER JOIN `components` ON componentsMap.component_id = components.id
INNER JOIN `tradeNamesMap` ON .tradeNamesMap.product_id = products.id
INNER JOIN `tradeNames` ON tradeNamesMap.tradeName_id = tradeNames.id
WHERE (((((LOWER(inci) LIKE '%abies%')
OR (trade_name.LOWER(name) LIKE '%abies%'))
OR (components.LOWER(no_cas)='abies'))
OR (components.LOWER(no_einecs)='abies'))
OR (components.LOWER(name)='abies'))
AND (`published`=1)
ORDER BY `trade_name`.`name` DESC
You don't need to list the tables before the INNER JOINs. In fact, simply don't ever use commas in the FROM clause. So:
SELECT COUNT(*)
FROM `products`
INNER JOIN `componentsMap` ON componentsMap.product_id = product.id
INNER JOIN `components` ON componentsMap.component_id = components.id
INNER JOIN `tradeNamesMap` ON tradeNamesMap.product_id = products.id
INNER JOIN `tradeNames` ON tradeNamesMap.tradeName_id = tradeNames.id
WHERE (((((LOWER(inci) LIKE '%abies%')
OR (trade_name.LOWER(name) LIKE '%abies%'))
OR (components.LOWER(no_cas)='abies'))
OR (components.LOWER(no_einecs)='abies'))
OR (components.LOWER(name)='abies'))
AND (`published`=1)
ORDER BY `trade_name`.`name` DESC;
The above query only returns one row because of the COUNT(). The order by suggests that you actually want this information for each trade_name.name. If so, you need a GROUP BY:
SELECT tn.name, COUNT(*)
FROM `products` p INNER JOIN
`componentsMap cm
ON cm.product_id = p.id INNER JOIN
`components` c
ON cm.component_id = c.id INNER JOIN
`tradeNamesMap` tnm
ON tnm.product_id = p.id INNER JOIN
`tradeNames` tn
ON tnm.tradeName_id = tn.id
WHERE ((LOWER(inci) LIKE '%abies%') OR
(tn.LOWER(name) LIKE '%abies%') OR
(c.LOWER(no_cas)='abies') OR
(c.LOWER(no_einecs)='abies') OR
(c.LOWER(name)='abies')
) AND
(`published` = 1)
GROUP BY tn.name
ORDER BY tn.`name` DESC
INNER JOIN `[components]` ON componentsMap.component_id = components.id
AND
SELECT COUNT(*) FROM `products`, [`components`], `tradeNames`
Two components are there.
Just guessing, and untested, but I suspect that something like this would do what you're after...
SELECT n.name
, COUNT(*)
FROM products p
JOIN componentsMap pc
ON pc.product_id = p.id
JOIN components c
ON c.id = pc.component_id
JOIN tradeNamesMap pn
ON pn.product_id = p.id
JOIN tradeNames n
ON n.id = pn.tradeName_id
WHERE
( inci LIKE '%abies%'
OR n.name LIKE '%abies%'
OR 'abies' IN (c.no_cas,c.no_einecs,c.name)
)
AND published = 1
GROUP
BY n.name
ORDER
BY n.name DESC

deleting 2 or more tables using join in mysql

Hi every one i have a project which i need to delete 4 tables at the same time using join in mysql
this is my query:
DELETE product_tbl, product_rebate, product_terms, product_cashp, product_downpayment
FROM product_tbl INNER JOIN product_rebate ON product_tbl.Prod_ID = product_rebate.Prod_ID
INNER JOIN product_terms ON product_tbl.Prod_ID = product_terms.Prod_ID
INNER JOIN product_cashp ON product_tbl.Prod_ID = product_tbl.Prod_ID;
it 's not working it gives me an error: #1109 - Unknown table 'product_tbl' in MULTI DELETE
Try this,
DELETE product_tbl, product_rebate, product_terms, product_cashp,
product_downpayment
FROM product_tbl
INNER JOIN product_rebate
ON product_tbl.Prod_ID = product_rebate.Prod_ID
INNER JOIN product_terms
ON product_tbl.Prod_ID = product_terms.Prod_ID
INNER JOIN product_cashp
ON product_tbl.Prod_ID = product_cashp.Prod_ID;
I think you've made mistake here in last inner join,
INNER JOIN product_cashp
ON product_tbl.Prod_ID = product_tbl.Prod_ID; -- both side same table?
MySQL DELETE JOIN with INNER JOIN
DELETE T1, T2
FROM T1
INNER JOIN T2 ON T1.key = T2.key
WHERE condition
Try this :-
DELETE product_tbl, product_rebate, product_terms, product_cashp,
product_downpayment
FROM product_tbl
INNER JOIN product_rebate
ON product_tbl.Prod_ID = product_rebate.Prod_ID
INNER JOIN product_terms
ON product_tbl.Prod_ID = product_terms.Prod_ID
INNER JOIN product_cashp
ON product_tbl.Prod_ID = product_cashp.Prod_ID;

UPDATE from two INNER JOINS

I want to update a column in table INVENTAR from a couple of INNER JOINS.
I have the following:
Table INVENTAR with the column PRODUCT_ID,CATEGORY
Table PRODUCT_TO_CATEGORY with the columns PRODUCT_ID,CATEGORY_ID
Table CATEGORY_DESCRIPTION with the columns CATEGORY_ID, NAME
I want the NAME column to update the CATEGORY column.
Here's my code:
UPDATE inventar
SET inventar.category=category_description.name
FROM inventar
INNER JOIN product_to_category
ON product_to_category.product_id=inventar.product_id
INNER JOIN category_description
ON category_description.category_id=product_to_category.category_id
The correct MySQL syntax is:
UPDATE inventar i INNER JOIN
product_to_category ptc
ON ptc.product_id = i.product_id INNER JOIN
category_description cd
ON cd.category_id = ptc.category_id
SET i.category = cd.name;
Your syntax looks more appropriate for SQL Server or Postgres.

error mysql query with inner join

select simplex_comunes.cod_color_piel.descripcion as cod_color_piel, simplex_comunes.cod_sexo.descripcion as cod_sexo, count(*)
from simplex_comunes.cod_color_piel,simplex_comunes.cod_sexo
inner join simplex_ch.dat_trabajadores on simplex_ch.dat_trabajadores.id_color_piel = simplex_comunes.cod_color_piel.codigo
inner join simplex_ch.dat_trabajadores on simplex_comunes.cod_sexo.codigo = simplex_ch.dat_trabajadores.id_sexo
group by simplex_comunes.cod_color_piel.descripcion,simplex_comunes.cod_sexo.descripcion
the error is Not unique table/alias: 'dat_trabajadores',
Please help, thanks!!!
I haven't checked if your query is "intelligent" or not, but you have to use aliases as you use twice dat_trabajadores in your query.
You have to tell MySQL which table you use in your JOIN.
select simplex_comunes.cod_color_piel.descripcion as cod_color_piel, simplex_comunes.cod_sexo.descripcion as cod_sexo, count(*)
from simplex_comunes.cod_color_piel,simplex_comunes.cod_sexo
inner join simplex_ch.dat_trabajadores tr1 on simplex_ch.tr1.id_color_piel = simplex_comunes.cod_color_piel.codigo
inner join simplex_ch.dat_trabajadores tr2 on simplex_comunes.cod_sexo.codigo = simplex_ch.tr2.id_sexo
group by simplex_comunes.cod_color_piel.descripcion,simplex_comunes.cod_sexo.descripcion
you have two same aliases in your query.
rename them differently
select simplex_comunes.cod_color_piel.descripcion as descripcion,
simplex_comunes.cod_sexo.descripcion as cod_sexo_descripcion,
count(*)
from simplex_comunes.cod_color_piel,simplex_comunes.cod_sexo
inner join simplex_ch.dat_trabajadores t1 on t1.id_color_piel = simplex_comunes.cod_color_piel.codigo
inner join simplex_ch.dat_trabajadores t2 on simplex_comunes.cod_sexo.codigo = t2.id_sexo
group by simplex_comunes.cod_color_piel.descripcion,simplex_comunes.cod_sexo.descripcion