I get the error ERROR 1066 (42000): Not unique table/alias: 'order_has_artikelgroup' I cant figure out whats wrong with it.
SELECT
`artikel`.`foto_naam`,
`fotografer`.`id`,
`fotografer`.`name_fotografer`,
`customer`.`first_name`,
`customer`.`last_name`,
`fotografer`.`domain_name`,
`fotografer`.`email`,
`order`.`invoice_no`,
`order`.`order_cost`,
`order`.`total_cost`,
`order`.`invoice_date`,
`order`.`payment`,
`order`.`status`
FROM
`artikel_group`
INNER JOIN `artikel` ON `artikel_group`.`id` = `artikel`.`artikelgroup_id`
INNER JOIN `fotografer` ON `artikel_group`.`fotografer_id` = `fotografer`.`id`
INNER JOIN `order_has_artikelgroup` ON `order_has_artikelgroup`.`order_id` = `order`.`id`
INNER JOIN `order_has_artikelgroup` ON `order_has_artikelgroup`.`artikelgroup_id` = `artikel_group`.`id`
INNER JOIN `customer` ON `order`.`customer_id` = `customer`.`id`
you've got two times a join on table
order_has_artikelgroup
INNER JOIN `order_has_artikelgroup` ON `order_has_artikelgroup`.`order_id` = `order`.`id`
INNER JOIN `order_has_artikelgroup` ON `order_has_artikelgroup`.`artikelgroup_id` = `artikel_group`.`id`
but every element queried must be unique.
So you should either add an alias
something like
INNER JOIN `order_has_artikelgroup` oha ON oha.`order_id` = `order`.`id`
INNER JOIN `order_has_artikelgroup` oha2 ON oha2 .`artikelgroup_id` = `artikel_group`.`id`
(and use oha and oha2 in your select clause also if you need them)
or change your join (depending on the logic of the query)
INNER JOIN `order_has_artikelgroup`
ON `order_has_artikelgroup`.`order_id` = `order`.`id` and
`order_has_artikel_group`.`artikelgroup_id` = `artikel_group`.`id`
Related
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
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;
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
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
I'm having a real mind blank - This code selects rows quite nicely apart from those entries where I change st.station_id from a value of '1' to a different (but still valid) number but where there are no entries for that station_id in either the station_owner_map table, the organisation table or the cap_gen_data_table. I basically need to amend my sql to ignore any table where there are no entries.
Select st.station_id, st.station_name , st.st_town, st.st_state, c1.country_name, o1.organisation_name, som1.equity, st.river_basin, st.cost, st.cost_ref, st.comm_year,cg1.caporgen, ht1.hydro_name, cg1.value, srs1.srs_description, cg1.ref_year
FROM station st
inner join station_country_map scm1 on st.station_id = scm1.station_id
inner join country c1 on scm1.country_id = c1.country_id
inner join station_owner_map som1 on st.station_id = som1.station_id
inner join organisation o1 on som1.owner_id = o1.org_id
inner join cap_gen_data cg1 on st.station_id = cg1.station_id
inner join value_lookup vl1 on cg1.caporgen = vl1.id
inner join hydro_type ht1 on cg1.hydro_type_id = ht1.type_id
inner join station_record_status srs1 on cg1.capacity_status = srs1.st_rec_stat_id
where st.station_id = 1
It's caused by your inner joins. Inner join means there has to be a value in both tables for the record to show up in the result set.
Use left join instead, then only the table 'on the left' has to have a value.
Use left join on tables where the value may not be present.
If you have two tables A and B an inner join will only return the rows from A where the join condition is met. A left join will return all rows from A regardless of if the join condition is satisfied. Columns in the select statement associated with B will be null when a left join is used.
I have only added the left join to the tables you have indicated. If other tables may not satisfy the join condition change the join type from inner to left.
Select st.station_id, st.station_name , st.st_town, st.st_state, c1.country_name, o1.organisation_name, som1.equity, st.river_basin, st.cost, st.cost_ref, st.comm_year,cg1.caporgen, ht1.hydro_name, cg1.value, srs1.srs_description, cg1.ref_year
FROM station st
inner join station_country_map scm1 on st.station_id = scm1.station_id
inner join country c1 on scm1.country_id = c1.country_id
left join station_owner_map som1 on st.station_id = som1.station_id
left join organisation o1 on som1.owner_id = o1.org_id
left join cap_gen_data cg1 on st.station_id = cg1.station_id
inner join value_lookup vl1 on cg1.caporgen = vl1.id
inner join hydro_type ht1 on cg1.hydro_type_id = ht1.type_id
inner join station_record_status srs1 on cg1.capacity_status = srs1.st_rec_stat_id
where st.station_id = 1