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;
Related
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
I have searched for days on how to get around this error while trying to update a field from a multiple join table, with a minimum date from the same mutiple join tableset.
This is my Update statement:
update vtiger_projectmilestone
Inner Join vtiger_projectmilestonecf ON vtiger_projectmilestone.projectmilestoneid = vtiger_projectmilestonecf.projectmilestoneid
Inner Join vtiger_crmentity ON vtiger_projectmilestone.projectmilestoneid = vtcrmm.crmid
inner join vtiger_project on vtiger_project.projectid = vtiger_projectmilestone.projectid
Inner Join vtiger_crmentity vtcrmp ON vtcrmp.crmid = vtiger_project.projectid
set vtiger_projectmilestone.projectmilestonedate =
(select min(vtiger_projecttaskcf.cf_779)
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid = tvptcf.projecttaskid
Inner Join vtiger_projectmilestone tvpm ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpmcf.projectmilestoneid
Inner Join vtiger_crmentity AS vtcrmm ON tvpm.projectmilestoneid = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
)
where vtiger_projectmilestone.projectid =
(select vtiger_project.projectid from vtiger_project
INNER JOIN vtiger_crmentity vtcrmp ON vtiger_project.projectid = vtcrmp.crmid
where vtcrmp.deleted = 0 order by vtiger_project.projectid desc limit 1)
and vtcrmp.deleted = 0
and vtcrmm.deleted = 0
and (vtiger_projectmilestone.projectmilestonedate is null or vtiger_projectmilestonecf.cf_763 is null) ;
This is a real life update query, not just a simple one table relationship.
I got round it by creating a temp table, inserting the value, updating the destination table and dropping the temp table.
I would really like to get this right, because it will come up more often.
All assistance is appreciated.
Cheers
Bernard Bailey
As stated in this answer you can't use the target update table in a subquery, as you can see in your query
SELECT min(vtiger_projecttaskcf.cf_779)
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid = tvptcf.projecttaskid
Inner Join
--using target update table in query
vtiger_projectmilestone tvpm ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpmcf.projectmilestoneid
Inner Join vtiger_crmentity AS vtcrmm ON tvpm.projectmilestoneid = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
However i think that a work around is using the data from the table that you're updating, so instead of using joins, you could write some where conditions for example:
SELECT min(vtiger_projecttaskcf.cf_779)
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid = tvptcf.projecttaskid
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_crmentity AS vtcrmm ON tvpt.projecttasknumber = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
--using the projectmilestoneid in a where clause
AND tvpt.projecttasknumber=vtiger_projectmilestone.projectmilestoneid
The caveat could be that probably you will get some performance issues, also, as I don't know the full schema, I can't tell if using other tables in the subquery instead of vtiger_projectmilestone will give you the right result
I am trying to update one table from 3 tables which is working fine, but i want to update a table from 3 after i insert data in 4th table, here what i am doing
INSERT INTO relation_table(cid,pid,liid,lnid,lgid,l_key)
SELECT a.cid,
a.pid,
b.liid,
c.lnid,
d.lgid,
md5(CONCAT(a.cid, a.pid, a.lurl, c.lnid, d.lang))
FROM links a
INNER JOIN links_table b
ON a.lurl = b.lurl
INNER JOIN lname_table c
ON a.lname = c.lname
INNER JOIN lang_table d
ON a.lang = d.lang
where a.checked != "3" limit 2;
update links SET checked='3'
WHERE lid=a.lid
Last statement is not working for update
Please help ;)
Try this:
UPDATE links a
INNER JOIN links_table b ON a.lurl = b.lurl
INNER JOIN lname_table c ON a.lname = c.lname
INNER JOIN lang_table d ON a.lang = d.lang
SET a.checked='3'
WHERE a.checked != '3' LIMIT 2;
I would like to update a table based on reading a value in another table.
UPDATE contacts_cstm set ld_dt_crtd_c = '2014-06-11 12:26:17'
WHERE **id = 'b0bc2ccf-ddfe-81b1-a278-53ba8bd0a93f'** AND deleted=0
However id is a column in contacts table.
UPDATE contacts_cstm
INNER JOIN contacts ON contacts.whatever_column = contacts_cstm.whatever_column
set ld_dt_crtd_c = '2014-06-11 12:26:17' WHERE id = 'b0bc2ccf-ddfe-81b1-a278-53ba8bd0a93f' AND deleted=0
You can Try this example
UPDATE
Sales_Import
SET
Sales_Import.AccountNumber = RAN.AccountNumber
FROM
Sales_Import SI
INNER JOIN
RetrieveAccountNumber RAN
ON
SI.LeadID = RAN.LeadID
UPDATE
UPDATE contacts_cstm
SET ld_dt_crtd_c = '2014-06-11 12:26:17'
FROM contacts_cstm cc
INNER JOIN contacts c ON cc.id = c.id
WHERE cc.deleted = 0
$rs = mysql_query("
SELECT a.id
FROM a
JOIN b ON b.id=a.id
WHERE
b.p1=1 AND
a.p1=1");
while($r = mysql_fetch_assoc($rs))
$ids[] = $r['id'];
$rs = mysql_query("
UPDATE a
SET p2=2
WHERE id IN (".implode(",",$ids).")");
How to do this in one query?
UPDATE a
JOIN b ON a.id=b.id AND a.p1 = 1 AND b.p1 = 1
SET a.p2 = 2
You can do it using INNER JOIN which performs better than IN clause:
UPDATE a
INNER JOIN b
ON b.id=a.id
AND b.p1 = 1
AND a.p1 = 1
SET p2 = 2;
UPDATE a
SET p2=2
WHERE id IN (SELECT a.id
FROM a
JOIN b ON b.id=a.id
WHERE
b.p1=1 AND
a.p1=1)