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
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
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
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
I cannot solve this issue, I have tree tables, inner join and I'm after selecting only specific columns because I'm concerned about performance issue if I just leave SELECT * to do the job.
I have tables: ugovori-artikli, ugovori and ids, working SELECT * query:
SELECT * FROM `ugovori-artikli`
INNER JOIN `ugovori`
ON `ugovori-artikli`.`ugovor_id` = `ugovori`.`id`
INNER JOIN `ids`
ON `ugovori`.`kupac_id` = `ids`.`id`
AND `ugovori-artikli`.`artikal` = ?
and non-working query, my attempt:
SELECT a.*,
b.*,
c.id, c.ime, c.prezime
FROM `ugovori-artikli` AS a
INNER JOIN `ugovori` AS b
ON `ugovori-artikli`.`ugovor_id` = `ugovori`.`id`
INNER JOIN `ids` AS c
ON `ugovori`.`kupac_id` = `ids`.`id`
AND `ugovori-artikli`.`artikal` = ?
I get an error:
Error in query (1054): Unknown column 'ugovori-artikli.ugovor_id' in 'on clause'
Use the alias name
SELECT a.*,
b.*,
c.id, c.ime, c.prezime
FROM `ugovori-artikli` AS a
INNER JOIN `ugovori` AS b
ON a.`ugovor_id` = b.`id`
INNER JOIN `ids` AS c
ON b.`kupac_id` = c.`id`
AND a.`artikal` = ?
Hope this helps :)
You have to use the aliasses not the table name in your SQL statement.
SELECT a.*,b.*,c.id, c.ime, c.prezime
FROM `ugovori-artikli` AS a
INNER JOIN `ugovori` AS b ON a.`ugovor_id` = b.`id`
INNER JOIN `ids` AS c
ON b.`kupac_id` = cid`
AND a.`artikal` = ?
I have query like this.
SELECT account.AccountNumber, account.Name, Sum(agro.price*agro.qty) AS Expr1
FROM account,data INNER JOIN (agro INNER JOIN data ON agro.BillNo = data.BillNo) ON
account.AccountNumber = data.acno
GROUP BY account.AccountNumber, account.Name;
I want to add where db='true' this columns is of 'data' table then how can i do pls help me?
Try this:
SELECT account.AccountNumber, account.NAME, Sum(agro.price * agro.qty) AS Expr1
FROM ((account
INNER JOIN data ON account.AccountNumber = data.acno)
INNER JOIN agro ON agro.BillNo = data.BillNo)
WHERE data.db='true'
GROUP BY account.AccountNumber, account.NAME;
You had some confusion in your JOINs, but i think this is what you were aiming for