You can't specify target table '...' for update in FROM clause - mysql

I want to make an update on a table with the results of a query and that records exist in the tables, my SLQ is:
UPDATE
reparticion
SET
responsable_nombre_completo = (select CONCAT_WS(',',persona.nombre,persona.apellido) FROM persona INNER JOIN usuario on usuario.cuil = persona.cuil)
WHERE
reparticion.id IN (select persona.reparticion_id FROM persona INNER JOIN usuario on usuario.cuil = persona.cuil INNER JOIN reparticion on reparticion.id = persona.reparticion_id);
but I get the following error:
You can't specify target table 'reparticion' for update in FROM clause

The better approach would be to use join update instead of sub-queries.
update reparticion r
join persona p on p.reparticion_id = r.id
join usuario u on u.cuil = p.cuil
set r.responsable_nombre_completo = CONCAT_WS(',',p.nombre,p.apellido)

Related

MySQL Error 1093 - Can't specify target table for update in FROM clause (two joins)

I'm trying to update the unit_price in a sales_order_parts detail table with calculation from the applied_discount field from sales_orders. Also, the price is in the master_part_list table as price. When I run from the select statement down, it runs fine and returns the list of values from order_number 209 with the correct calculation. When I run it complete from the update line, it returns "Error Code: 1093. You can't specify target table 'sop' for update in FROM clause" Any ideas?
update sales_order_parts as sop
set unit_price =
(select (master_part_list.price * (1-(so.applied_discount/100)))
from sales_orders as so
inner join sales_order_parts as sop2
on so.id = sop2.order_id
inner join master_part_list
on sop2.part_id = master_part_list.id
where so.order_number = 209);
You could try with a join without subquery
update sales_order_parts as sop
INNER JOIN sales_orders as so on so.id = sop.order_id
AND so.order_number = 209
inner join master_part_list on sop.part_id = master_part_list.id
SET sop.unit_price = master_part_list.price * (1-(so.applied_discount/100))
Use a subquery for sales_order_parts. mysql then treats as new temporary table
Like
update sales_order_parts as sop
set unit_price =
(select (master_part_list.price * (1-(so.applied_discount/100)))
from sales_orders as so
inner join (SELECT * FROM sales_order_parts) as sop2
on so.id = sop2.order_id
inner join master_part_list
on sop2.part_id = master_part_list.id
where so.order_number = 209);

MySQL Select and Join - Ambiguous column

I'm trying to join four tables together. The code works fine when I'm only comparing the EVENT_DATA and joining the PERSONA tables as I'm able to get the "Name" column from PERSONA (which doesn't exist in the EVENT_DATA table). However, one of the problems is that this "Name" column also exists in the CUSTOMCAR table, so I can only get one or the other. Additionally, when I tried adding the last join statement, the code wouldn't run at all.
If someone could please help me, that would be great!
$sql = "SELECT * FROM EVENT_DATA
LEFT JOIN PERSONA ON EVENT_DATA.personaId = PERSONA.ID
LEFT JOIN CUSTOMCAR ON CUSTOMCAR.ownedCarId = EVENT_DATA.carId
LEFT JOIN CARCLASSES ON CARCLASSES.store_name = CUSTOMCAR.name
WHERE (EVENT_DATA.EVENTID = '299')";
You should avoid * and use explicit columns list instead:
SELECT EVENT_DATA.personaId, ...
FROM EVENT_DATA
LEFT JOIN PERSONA ON EVENT_DATA.personaId = PERSONA.ID
LEFT JOIN CUSTOMCAR ON CUSTOMCAR.ownedCarId = EVENT_DATA.carId
LEFT JOIN CARCLASSES ON CARCLASSES.store_name = CUSTOMCAR.name
WHERE (EVENT_DATA.EVENTID = '299');
If you have same column name you need to to use aliasing:
SELECT ...
CUSTOMCAR.NAME AS c_name,
PERSONA.NAME AS p_name
...
You could also use Aliasing:
$sql = "SELECT ed.*,
pa.*,
cc.*,
ccs.*
FROM EVENT_DATA AS ed
LEFT JOIN PERSONA AS pa ON ed.personaId = pa.ID
LEFT JOIN CUSTOMCAR AS cc ON cc.ownedCarId = e.carId
LEFT JOIN CARCLASSES AS ccs ON ccs.store_name = cc.name
WHERE (ed.EVENTID = '299')";
Note: Although as suggested by #Lukasz, you should really avoid using wildcard (*), and provide an explicit list of columns in the SELECT clause.

How to access outer table data in join where

How can I access data from an outer table in a SELECT, and use it in an WHERE inside a JOIN estructure?
Below is the current query:
SELECT
cvl.id caracteristica_valor_id,
cvl.nome caracteristica_valor_nome,
cvl.valor caracteristica_valor_valor,
ctp.id caracteristica_tipo_id,
ctp.nome caracteristica_tipo_nome,
ctp.codigo caracteristica_tipo_codigo,
ctp.tipo caracteristica_tipo_tipo,
COUNT(DISTINCT var.id_perfil_produto) quantidade_itens
FROM
caracteristica_variacao cvr
INNER JOIN caracteristica_valor cvl ON cvl.id = cvr.id_caracteristica_valor
INNER JOIN caracteristica_tipo ctp ON ctp.id = cvl.id_caracteristica_tipo
INNER JOIN variacao var ON var.id = cvr.id_variacao
INNER JOIN(
SELECT DISTINCT
ppr.id perfil_produto_id
FROM
perfil_produto ppr
INNER JOIN produto pro ON pro.id = ppr.id_produto
INNER JOIN(
SELECT ppr2.id AS id_perfil_sub,a
COUNT(var.id) AS qtd_variacoes,
SUM(var.quantidade_estoque) AS quantidade_estoque,
COALESCE(SUM(var.quantidade_estoque_reservada),0) AS quantidade_estoque_reservada,
MIN(var.disponibilidade) AS disponibilidade,
MIN(var.frete_gratis) AS frete_gratis,
MIN(var.preco_venda) AS preco_venda,
MAX(var.preco_listagem) AS preco_listagem
FROM
variacao var
LEFT JOIN perfil_produto ppr2 ON ppr2.id = var.id_perfil_produto
LEFT JOIN caracteristica_variacao cvr_1 ON cvr_1.id_variacao = var.id
LEFT JOIN caracteristica_valor cvl_1 ON cvl_1.id = cvr_1.id_caracteristica_valor
LEFT JOIN caracteristica_tipo ctp_1 ON ctp_1.id = cvl_1.id_caracteristica_tipo
WHERE
var.disponibilidade = 1
AND(
ctp_1.codigo = 'tamanho' AND cvl_1.valor IN('p')
)
GROUP BY
ppr2.id
) AS grp_var ON grp_var.id_perfil_sub = ppr.id
INNER JOIN produto_categoria prc ON pro.id = prc.produto_id
INNER JOIN categoria cat ON prc.categoria_id = cat.id
WHERE
pro.disponibilidade = 1 AND prc.categoria_id IN (164, 165, 166)
) AS produto ON produto.perfil_produto_id = var.id_perfil_produto
GROUP BY
cvl.id
ORDER BY
ctp.tipo ASC,
ctp.id
I need the field ctp.codigo from the outer table inside thist part:
WHERE
var.disponibilidade = 1
AND(
ctp_1.codigo = 'tamanho' AND cvl_1.valor IN('p')
)
for this section to be as follows:
WHERE
var.disponibilidade = 1
AND(
(ctp.codigo != 'tamanho' AND ctp_1.codigo = 'tamanho' AND cvl_1.valor IN('p'))
OR
(ctp.codigo = 'tamanho')
)
It's not possible to reference columns from the outer query from inside an inline view query.
In the MySQL venacular, the inline view query is called a "derived table". And that name makes sense, because of the way MySQL processes it. The execution plan first materializes the inline view query into a temporary(-ish) table. Once that is done, then the outer query can run, referencing the contents of the derived table.
MySQL doesn't have available the columns from the outer query at the time the inline view query runs.
It is possible to reference columns from the outer query inside a subquery that appears for example in the SELECT list, or in the WHERE clause. We call a subquery that references columns from outer query a "correlated subquery".

MySQL inner join w/ alias

So this is my table DENUNCIAS
id_denuncia,
id_categoria,
id_fecha,
email_denunciante -- FK table usuarios,
email_denunciado -- FK table usuarios,
descripcion,
fecha
As you can see i have a double reference to the same table.
What i'm trying to do is select (inner join) 'usuarios.nombreCompleto' twice, once for 'email_denunciante' and the other for 'email_denunciado' . This is where i am so far:
select
categorias.nombreCorto as 'Categoria',
fechas.circuito as 'Fecha',
usuarios.nombreCompleto as usuDenunciante,
usuarios.nombreCompleto as usuDenunciado,
denuncias.descripcion as 'Detalle',
denuncias.fecha as 'Enviada el'
from denuncias
inner join categorias on denuncias.id_categoria = categorias.id_categoria
inner join fechas on denuncias.id_fecha = fechas.id_fecha
inner join usuarios as usuDenunciante on denuncias.email_denunciante = usuarios.email
inner join usuarios as usuDenunciado on denuncias.email_denunciado = usuarios.email;
And this is the error i get:
Error Code: 1054. Unknown column 'usuarios.nombreCompleto' in 'field list'
I'd tried several methods with negative results.
Since it's a sintaxis error, not logic, i decided not to translate the code.
Thanks in advance!
You create table alias, you must use then on your select list. usuarios must be usuDenunciante or usuDenunciado.
Try this:
select categorias.nombreCorto as 'Categoria',
fechas.circuito as 'Fecha',
usuDenunciante.nombreCompleto as usuDenunciante,
usuDenunciado.nombreCompleto as usuDenunciado,
denuncias.descripcion as 'Detalle',
denuncias.fecha as 'Enviada el'
from denuncias
inner join categorias
on denuncias.id_categoria = categorias.id_categoria
inner join fechas
on denuncias.id_fecha = fechas.id_fecha
inner join usuarios as usuDenunciante
on denuncias.email_denunciante = usuDenunciante.email
inner join usuarios as usuDenunciado
on denuncias.email_denunciado = usuDenunciado.email;
I'm assuming that your usarious table really do contain the nombreCompleto column. If this is correct, I think the problem is, that you reference the usarious table directly instead of using the aliases you have made. Because you access that table twice, you need to specify which alias you mean to SELECT from.
Fx:
SELECT categorias.nombreCorto as 'Categoria',
fechas.circuito as 'Fecha',
usuDenunciante.nombreCompleto.as usuDenunciante,
usuDenunciado.nombreCompleto as usuDenunciado,
denuncias.descripcion as 'Detalle',
denuncias.fecha as 'Enviada el'
from denuncias inner join categorias on denuncias.id_categoria = categorias.id_categoria
inner join fechas on denuncias.id_fecha = fechas.id_fecha
inner join usuarios as usuDenunciante on denuncias.email_denunciante = usuarios.email
inner join usuarios as usuDenunciado on denuncias.email_denunciado = usuarios.email;

SQL query for returning multiple fields joined to the same reference

So I have the following table, do_stock_movement, that looks like this:
stock_movement_id sm_number sm_source_id sm_destination_id
15164b86a7533d 145 1516478840ee29 151644d8bd63f2
15166b89d1a9fc 194 15165c481bd9d0 151659e632cd48
The columns sm_source_id and sm_destination_id both reference product points stored in do_product_points.
I'm using the following SQL query:
SELECT * FROM do_stock_movement
INNER JOIN do_product_points ON product_points_id = sm_source_id
WHERE sm_number = '145'
In do_product_points, there's a field called pp_name. I need the corresponding pp_name for both sm_source_id and sm_destination_id.
However, the query above will only return the pp_name for sm_source_id, or for sm_destination_id if you change the joined field to that.
What SQL query will return the corresponding pp_name for both sm_source_id and sm_destination_id?
I hope this is clear. Please ask questions if it isn't!
JOIN this table do_product_points one more times for the sm_destination_id:
SELECT
s.pp_name AS SourcePoint,
d.pp_name AS DestinationPoint,
...
FROM do_stock_movement AS m
INNER JOIN do_product_points s ON s.product_points_id = m.sm_source_id
INNER JOIN do_product_points d ON d.product_points_id = m.sm_destination_id
WHERE m.sm_number = '145'
You need join twice and use alias:
SELECT *, Src.pp_name, Dst.pp_name FROM do_stock_movement
INNER JOIN do_product_points as Src
ON Src.product_points_id = sm_source_id
INNER JOIN do_product_points as Dst
ON Dst.product_points_id = sm_destination_id
You need to join to the product_points table twice, once with source_id and once with destination_id:
SELECT * FROM do_stock_movement move
INNER JOIN do_product_points source ON source.product_points_id = move.sm_source_id
INNER JOIN do_product_points dest ON dest.product_points_id = move.sm_destination_id
WHERE sm_number = '145'