Inner join instead of what I have now - mysql

The query below does what I want but takes a long time to finish. I have been trying to write it with INNER JOIN instead but can not figure out how to do it.
UPDATE cf_ab_companies
SET cf_ab_companies.col_330 = (
SELECT aaa_items.amount
FROM aaa_items
WHERE aaa_items.customer = cf_ab_companies.model_id
AND aaa_items.sku = 10
);
cf_ab_companies.model_id is unique value in the table.
There can be several records where aa_items.customer = 314 , but aa_items.customer = 314 AND aa_item.sku = 10 may only occur once.
Hope you understand what I mean. Thanks for all help.

UPDATE Multiple-table syntax can be found https://dev.mysql.com/doc/refman/8.0/en/update.html
using this your query would look like
UPDATE cf_ab_companies join
aaa_items on aaa_items.customer = cf_ab_companies.model_id
SET cf_ab_companies.col_330 = aaa_items.amount
WHERE aaa_items.sku = 10
;

Here is the MySQL UPDATE ... JOIN ... SET syntax:
UPDATE cf_ab_companies c
INNER JOIN aaa_items i ON i.customer = c.model_id AND i.sku = 10
SET c.cf_ab_companies.col_330 = i.aaa_items.amount

update cf_ab_companies c
join aaa_items a on a.customer = c.model_id
set c.col_330 = a.amount
where a.sku = 10
......

I believe an UPDATE FROM can help:
UPDATE cf_ab_companies
SET cf_ab_companies.col_330 = aaa_items.amount
FROM cf_ab_companies INNER JOIN aaa_items
ON cf_ab_companies.model_id = aaa_items.customer AND aaa_items.sku=10

Related

Update data using join query in mysql

The below query is working fine but not updating records in the database. In my database there is three records available for update, but it's not updating.
UPDATE vgm_details VD SET VD.job_id = ( select S.job_id from stuffings S JOIN vgm V ON S.booking_id = V.booking_id WHERE S.container_no = VD.container_no) where VD.job_id = 0;
i have attached screenshot of vgm_details table.
Instead of subquery (and related issue for scoping) You could use an update with join
UPDATE vgm_details VD
INNER JOIN stuffings S ON S.container_no = VD.container_no
INNER JOIN vgm V ON S.booking_id = V.booking_id
SET VD.job_id = S.job_id
WHERE VD.job_id = 0;

not able to update to update table as it is used in from clause in subquery

UPDATE qn_answers
SET `GRADE` = -100
WHERE `QSN_ID` IN (SELECT q.`ID` FROM `questions` q
LEFT JOIN qn_answers qa ON q.`ID` = qa.`QSN_ID`
WHERE q.`QSN_TYPE_ID` = 3 AND qa.`ISCORRECT` = 0 AND qa.`GRADE` = 0);
I'm trying to update a column based on the result from the join table , but it gives ERROR : 1093
I think you want an update join here, without a subquery:
UPDATE qn_answers qa
INNER JOIN questions q
ON qa.QSN_ID = q.ID
SET GRADE = -100
WHERE
q.QSN_TYPE_ID = 3 AND
qa.ISCORRECT = 0 AND
qa.GRADE = 0;
The error message has to do with that you are using the target table qn_answers as part of the join. You can't do this directly in a MySQL update.
use join update
UPDATE qn_answers a
join
(SELECT q.`ID` FROM `questions` q
LEFT JOIN qn_answers qa ON q.`ID` = qa.`QSN_ID`
WHERE q.`QSN_TYPE_ID` = 3 AND qa.`ISCORRECT` = 0 AND qa.`GRADE` = 0
) b on a.`QSN_ID`=b.`ID`
SET `GRADE` = -100
I think standard update also will work here. Try this:
UPDATE qa
SET `GRADE` = -100
FROM
`questions` q
LEFT JOIN
`qn_answers` qa
ON
q.`ID` = qa.`QSN_ID`
WHERE q.`QSN_TYPE_ID` = 3 AND qa.`ISCORRECT` = 0 AND qa.`GRADE` = 0;

UPDATE table whith specific condition

I have 2 tables - products and product_categories.
These tables JOIN ON products.product_id = product_categories.product_id .
I want to UPDATE field published in table products which have condition product_categories.product_categories = 100 .
Try this
UPDATE bjvui_virtuemart_products as prod
INNER JOIN bjvui_virtuemart_product_categories as cat
ON prod.virtuemart_product_id =cat.virtuemart_product_id
SET prod.published ={value}
WHERE cat.virtuemart_category_id=100
Use UPDATE with JOIN
UPDATE TABLEA a
JOIN TABLEB b ON a.join_colA = b.join_colB
SET a.columnToUpdate = [something]
WHERE a.someColumn = [some_value]
For your case
UPDATE bjvui_virtuemart_products AS p
INNER JOIN bjvui_virtuemart_product_categories AS c ON
p.virtuemart_product_id = c.virtuemart_product_id
SET p.published = "your_value"
WHERE c.virtuemart_category_id = 100;

how update decimal fields from one table to other in mysql

Fields lng and lat are decimal fields in each table.
update s
join t on s.id = t.id
set s.lat = t.lat , s.lng = t.lng
where s.id = t.id and (conditions)
In same select-query after running this update-query I not see difference - seem that UPDATE didn't run.
Someone have idea ?...
Try this, put conditions first and set after that
UPDATE s
JOIN t on s.id = t.id and (conditions)
SET s.lat = t.lat , s.lng = t.lng

Inner Join Nested Inside Update Statement SQL

I am trying to write a query that performs an inner join within an update statement.
Here is the current query I am working with:
UPDATE
singulation1.`q096_cq33r08-ds01-n-testtable`
SET
visual = t2.visual,
inspection_status = t2.inspection_status,
inspector_name = t2.inspector_name
FROM
singulation1.`q096_cq33r08-ds01-n-testtable` t1
INNER JOIN
singulation1.`q014_bq31t05-dw30-x` t2
ON
(t1.print = t2.print AND t1.id3 = t2.id3);
Something about MySql does not like the FROM clause.
For updates, you specify the join in the update clause
UPDATE
singulation1.`q096_cq33r08-ds01-n-testtable` AS t1
INNER JOIN singulation1.`q014_bq31t05-dw30-x` AS t2
ON t1.print = t2.print AND t1.id3 = t2.id3
SET
t1.visual = t2.visual
t1.inspection_status = t2.inspection_status,
t1.inspector_name = t2.inspector_name