How to update two columns from one select - mysql

Can I combine this in to one?
UPDATE 1_packages k
SET
rrp = (SELECT
SUM(u.quantity * p.rrp) * (sum(p.price_each <> 0 and p.rrp = 0) = 0)
FROM
1_packages_plu u
INNER JOIN
1_products p ON u.fk_products_id = p.id
WHERE
fk_packages_id = k.id)
;
UPDATE 1_packages k
SET
rrp_inc = (SELECT
SUM(u.quantity * p.rrp_inc) * (sum(p.price_each <> 0 and p.rrp_inc = 0) = 0)
FROM
1_packages_plu u
INNER JOIN
1_products p ON u.fk_products_id = p.id
WHERE
fk_packages_id = k.id)
;

It's simple, Try
UPDATE 1_packages k
SET rrp = (SELECT SUM(u.quantity * p.rrp) * (sum(p.price_each <> 0 and p.rrp = 0) = 0)
FROM 1_packages_plu u INNER JOIN 1_products p ON u.fk_products_id = p.id
WHERE fk_packages_id = k.id),
rrp_inc = (SELECT SUM(u.quantity * p.rrp_inc) * (sum(p.price_each <> 0 and p.rrp_inc = 0) = 0)
FROM 1_packages_plu u INNER JOIN 1_products p ON u.fk_products_id = p.id
WHERE fk_packages_id = k.id)

Try :
UPDATE table_name
SET column_one = "value one",
column_two = "value two"
WHERE condition

Related

MySQL query with multiple INNER JOIN returns duplicate entries in the result

I have the following data structure:
There are multiple tables:
I need the following return: sum of balance (sum(a.balance) as pnl) from tabposition.
However, this query returns duplicate results. Can anybody help me with this? Thanks
SET #dt_inicio := '2021-12-01';
SET #dt_fim := '2021-12-09';
select a.data, a.fund, 'CASH-USD' as ticker , 'CASH' as strategy, 'Dolar Cash' as company,
'Others' as type, 'Others' as sector,
sum(a.balance) as pnl from tabposition as a
inner join tablistaativos as c on a.ticker = c.ticker
inner join tabdiautil as d on a.data = d.data_util
inner join tabhistorynav as e on a.data = e.data
inner join tabhistorynav as f on d.data_util_ant = f.data
inner join tabprice as g on a.data = g.data
inner join tabprice as i on d.data_util_ant = i.data
inner join tabhistorynav as j on d.data_util_ant = j.data and a.fund = j.fund
inner join tabfatorx as l on a.data = l.data_pos
where a.data >= #dt_inicio and a.data <= #dt_fim and a.fund = 'TORK FUND' and a.qnt <> 0
and (c.class = 'ACCO' or c.class = 'ACCOUNTING' or c.class = 'SPAC' or c.class = 'ACOES' or
c.class = 'OPCOES' or c.class = 'FUNDO')
and e.official = 'Yes' and e.fund = 'TORK FIA' and f.official = 'Yes' and f.fund = 'TORK FIA'
and g.ticker = 'usdbrl' and i.ticker = 'usdbrl'
and l.cotista = 'TORK FIA'
group by a.data
order by a.data

case compare on MySQL does not match

I try to compare the variable #ecart who was a result of a subqueries.
I tryed to convert my variable in DECIMAL, but that change nothing.
SET #ecart := 0;
SELECT CASE #ecart := (TIME_TO_SEC(t.temps) - TIME_TO_SEC((
SELECT MIN(t.temps) as Temps
FROM temps t
INNER JOIN inscription i ON t.ide_inscription = i.id AND i.ide_calendrier = 3
INNER JOIN type_temps tt ON tt.id = t.ide_type_temps AND tt.intitule = 'Temps Final')))
WHEN #ecart = 0 THEN ""
WHEN #ecart<60 THEN CONCAT('+',#ecart)
WHEN #ecart BETWEEN 60 AND 3600 THEN CONCAT('+',SEC_TO_TIME(#ecart))
ELSE CONCAT('++',#ecart)
END as Ecart
FROM dag_evenement.temps t
INNER JOIN dag_evenement.inscription i ON t.ide_inscription = i.id AND i.ide_calendrier = 3
INNER JOIN dag_evenement.type_temps tt ON tt.id = t.ide_type_temps AND tt.intitule = 'Temps Final'
INNER JOIN dag_ticketing2.personne p ON i.ide_personne = p.IDPersonne
ORDER BY t.temps ASC;
and the result was
+0
++15
++58
++125
++141
++157
++209
++226
++240
++271
++283
++288
++290
++296
++298
Thanks for your help !

subquery returns more than 1 row?

subquery returns more than 1 row ? sulution ?
SELECT `t_files`.*, `t_users`.`username`,
(SELECT CONCAT(b.first_name, " ", b.last_name)
FROM t_files AS a
JOIN t_users as b
ON b.id = a.user_id
) as upload_by
FROM `t_files`
LEFT JOIN `t_files_permission`
ON `t_files_permission`.`id_files` = `t_files`.`file_id`
LEFT JOIN `t_users`
ON `t_users`.`id` = `t_files_permission`.`id_users`
WHERE `t_files`.`company_id` = '1'
AND `t_files_permission`.`id_users` = '59'
AND `is_deleted` =0
Add the condition in your sub query like below
SELECT t_files.*, t_users.username,
(SELECT CONCAT(b.first_name, " ", b.last_name)
FROM t_files AS a
JOIN t_users AS b ON b.id = a.user_id
WHERE a.id_users` = '59'
) as upload_by
FROM t_files
LEFT JOIN t_files_permission ON t_files_permission.id_files = t_files.file_id
LEFT JOIN t_users ON t_users.id = t_files_permission.id_users
WHERE t_files.company_id = '1' AND t_files_permission.id_users = '59' AND is_deleted =0
try this way
SELECT `t_files`.*, `t_users`.`username`,
(select CONCAT(b.first_name, " ", b.last_name)
from t_files as a
join t_users as b on b.id = a.user_id
where t.files.id=a.id) as upload_by
FROM `t_files` as
LEFT JOIN `t_files_permission` ON `t_files_permission`.`id_files` = `t_files`.`file_id`
LEFT JOIN `t_users` ON `t_users`.`id` = `t_files_permission`.`id_users`
WHERE `t_files`.`company_id` = '1' AND `t_files_permission`.`id_users` = '59' AND `is_deleted` =0
I think you don't need sub query. You can use join to resolve stuff.

Why MYSQL doesn't use my indexes?

I have table karton00 which holds thousands of records. This makes the queries slow. I would like to use indexes, but even though I set them, they are not being used.
Here is my table. and here is the result of using EXPLAIN.
This is my query:
SELECT '' checkbox,
k00_eszkozid,
k00_eszkoznev,
k01_gyariszam,
k01_leltszam,
prsn_name,
k00_status,
tstikon,
k00_startflow,
tstname,
k00_eszkozid,
k01_gyariszam,
k01_leltszam
FROM karton00
LEFT JOIN karton01
ON k00_eszkozid = k01_eszkozid
AND k01_status = 'A'
LEFT JOIN karton02
ON k00_eszkozid = k02_eszkozid
AND k02_status = 'A'
LEFT JOIN karton04
ON k00_eszkozid = k04_eszkozid
AND k04_status = 'A'
LEFT JOIN karton05
ON k00_eszkozid = k05_eszkozid
AND k05_status = 'A'
LEFT JOIN karton06
ON k00_eszkozid = k06_eszkozid
AND k06_status = 'A'
LEFT JOIN karton08
ON k00_eszkozid = k08_eszkozid
AND k08_status = 'A'
LEFT JOIN karton09
ON k00_eszkozid = k09_eszkozid
AND k09_status = 'A'
LEFT JOIN telephely et
ON k06_telephelyid = et.telepid
LEFT JOIN ktghely00 ek
ON k02_ktghid = ek.ktghid
LEFT JOIN person00 us
ON k06_userid = prsn_id
LEFT JOIN ktghely00 fk
ON prsn_ktgh = fk.ktghid
LEFT JOIN telephely ft
ON prsn_telep = ft.telepid
LEFT JOIN tamstatus
ON k02_tamstatusid = tstid
LEFT JOIN szakleltar
ON k02_szakleltarid = szleltid
LEFT JOIN tamszerv
ON k02_tamszervid = tszid
LEFT JOIN ktghely01
ON k02_vgazdaid = vgid
AND vgstatus = 'A'
LEFT JOIN ktghely00 vk
ON vgktghid = vk.ktghid
LEFT JOIN szallitok m
ON k05_mincegid = m.szallid
LEFT JOIN szallitok s
ON k04_szallitoid = s.szallid
LEFT JOIN dctnry00 ym
ON k05_minosit = ym.dctnryname
AND ym.dctnrygrp = 'YESNO'
LEFT JOIN dctnry00 yd
ON k05_check = yd.dctnryname
AND yd.dctnrygrp = 'YESNO'
WHERE k00_status IN ( 'A', 'H' )
AND ( k02_vgazdaid IN ( 7 ) )
AND NOT EXISTS (SELECT *
FROM eszkozkizar
WHERE ez_flid = 0
AND ez_emintaid = k00_eszkozmintaid
AND k00_uzembedatum < ( Date_add(Curdate(),
INTERVAL ez_honap month) )
AND k00_uzembedatum != ''
AND k02_vgazdaid NOT IN ( 7 ))
AND NOT EXISTS(SELECT *
FROM eszkozkizar
WHERE ez_flid = 0
AND ez_emintaid = k00_eszkozmintaid
AND k02_tamszervid = ez_tamszerv
AND k02_tamstatusid = ez_tamsts
AND k00_uzembedatum < ( Date_add(Curdate(),
INTERVAL ez_honap month) )
AND k00_uzembedatum != ''
AND k02_vgazdaid NOT IN ( 7 ))
ORDER BY k00_eszkoznev ASC;
Also you can use USE INDEX (index_list) statement: http://dev.mysql.com/doc/refman/5.5/en/index-hints.html

How to use "ALIAS" in "WHERE" in MYSQL

I'm not getting to use "ALIAS" in the "WHERE" MYSQL, in code below I need to do something like:
AND ( custo_percapita_regiao + comissao_representante ) < consig.valor_pago_comissao_representante
========================== code ===========================
SELECT
`consig`.`id`,
`consig`.`referencia_prod`,
`consig`.`valor_liquido`,
`consig`.`percentual_comissao_representante`,
`consig`.`valor_pago_comissao_representante`,
`consult`.`id` AS id_consultor,
`consult`.`nome` nome_consultor,
`repres`.`id` AS id_representante,
`repres`.`nome` nome_representante,
(
( SELECT ( `custo_alimentacao` + `custo_gasolina` + `custo_hospedagem` + `outros_custos` )
FROM `regioes`
WHERE `id` = `consult`.`regiao`
) /
( SELECT COUNT(*)
FROM `consignacoes`
INNER JOIN `consultores` ON `consultores`.`id` = `consignacoes`.`consultor`
WHERE `consultores`.`regiao` = `consult`.`regiao`
AND `consignacoes`.`excluido` = "N"
AND `consignacoes`.`valor_primeiro_pagamento` > 0
)
) AS custo_percapita_regiao,
(`consig`.`valor_liquido` * `consig`.`percentual_comissao_representante` / 100 ) AS comissao_representante
FROM `consignacoes` `consig`
INNER JOIN `consultores` `consult` ON `consult`.`id` = `consig`.`consultor`
INNER JOIN `administradores` `repres` ON `repres`.`id` = `consig`.`representante`
WHERE `representante` = 3
AND `consig`.`excluido` = "N"
AND `consig`.`valor_primeiro_pagamento` > 0
ORDER BY `consult`.`regiao` ASC, `consult`.`nome`
Possibly like this:-
SELECT
`consig`.`id`,
`consig`.`referencia_prod`,
`consig`.`valor_liquido`,
`consig`.`percentual_comissao_representante`,
`consig`.`valor_pago_comissao_representante`,
`consult`.`id` AS id_consultor,
`consult`.`nome` nome_consultor,
`repres`.`id` AS id_representante,
`repres`.`nome` nome_representante,
Sub1.RegionTot / Sub2.RegionCount AS custo_percapita_regiao,
(`consig`.`valor_liquido` * `consig`.`percentual_comissao_representante` / 100 ) AS comissao_representante
FROM `consignacoes` `consig`
INNER JOIN `consultores` `consult` ON `consult`.`id` = `consig`.`consultor`
INNER JOIN `administradores` `repres` ON `repres`.`id` = `consig`.`representante`
LEFT OUTER JOIN
(
SELECT `id`, ( `custo_alimentacao` + `custo_gasolina` + `custo_hospedagem` + `outros_custos` ) AS RegionTot
FROM `regioes`
) Sub1 ON Sub1.id = `consult`.`regiao`
LEFT OUTER JOIN
(
SELECT `consultores`.`regiao`, COUNT(*) AS RegionCount
FROM `consignacoes`
INNER JOIN `consultores` ON `consultores`.`id` = `consignacoes`.`consultor`
WHERE `consignacoes`.`excluido` = "N"
AND `consignacoes`.`valor_primeiro_pagamento` > 0
GROUP BY `consultores`.`regiao`
) Sub2 ON Sub2.`regiao` = `consult`.`regiao`
WHERE `representante` = 3
AND `consig`.`excluido` = "N"
AND `consig`.`valor_primeiro_pagamento` > 0
AND (Sub1.RegionTot / Sub2.RegionCount) < (`consig`.`valor_liquido` * `consig`.`percentual_comissao_representante` / 100 )
ORDER BY `consult`.`regiao` ASC, `consult`.`nome`
(partly swapping the sub selects from correlated ones in the SELECT to joins so the values can easily be used)
Try using variables:
SET #custo_percapita_regiao = 0;
SET #comissao_representante = 0;
SELECT
`consig`.`id`,
`consig`.`referencia_prod`,
`consig`.`valor_liquido`,
`consig`.`percentual_comissao_representante`,
`consig`.`valor_pago_comissao_representante`,
`consult`.`id` AS id_consultor,
`consult`.`nome` nome_consultor,
`repres`.`id` AS id_representante,
`repres`.`nome` nome_representante,
#custo_percapita_regiao :=
(
( SELECT ( `custo_alimentacao` + `custo_gasolina` + `custo_hospedagem` + `outros_custos` )
FROM `regioes`
WHERE `id` = `consult`.`regiao`
) /
( SELECT COUNT(*)
FROM `consignacoes`
INNER JOIN `consultores` ON `consultores`.`id` = `consignacoes`.`consultor`
WHERE `consultores`.`regiao` = `consult`.`regiao`
AND `consignacoes`.`excluido` = "N"
AND `consignacoes`.`valor_primeiro_pagamento` > 0
)
) AS custo_percapita_regiao,
#comissao_representante := (`consig`.`valor_liquido` * `consig`.`percentual_comissao_representante` / 100 ) AS comissao_representante
FROM `consignacoes` `consig`
INNER JOIN `consultores` `consult` ON `consult`.`id` = `consig`.`consultor`
INNER JOIN `administradores` `repres` ON `repres`.`id` = `consig`.`representante`
WHERE `representante` = 3 AND (#custo_percapita_regiao + #comissao_representante ) < consig.valor_pago_comissao_representante
AND `consig`.`excluido` = "N"
AND `consig`.`valor_primeiro_pagamento` > 0
ORDER BY `consult`.`regiao` ASC, `consult`.`nome`