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
Related
I have three tables: contact, account and account_contact. account_contact has the relation between the contact and the account.
In the account table I have the field account_contacts where I have the count of contacts for each account. I can run an sql that updates this value but I want to update it everytime I create or update the contact table.
This is the update sql:
update account a
left join (
select ac.account_id as a_id, count(c.id) as contacts
from account_contact ac
inner join contact c
on ac.contact_id = c.id
where c.deleted = 0
group by ac.account_id
) as n
ON a.id = n.a_id
SET a.account_contacts = n.contacts
This is the code I tryed:
CREATE TRIGGER `contact_new` AFTER INSERT ON `contact`
FOR EACH ROW update account a set a.account_contacts = a.account_contacts + 1
where a.id = (select ac.account_id from account_contact ac where ac.contact_id = NEW.id)
The answers I found in stackoverflow about trigger have only two tables.
Ok, done ! This is the code, it wasn't too diferent from the update.
CREATE TRIGGER `contact_new` AFTER INSERT ON `contact`
FOR EACH ROW update account a
left join (
select ac.account_id as a_id
from account_contact ac
inner join contact c
on ac.contact_id = c.id
where ac.contact_id = NEW.id
) as n
ON a.id = n.a_id
SET a.account_contacts = a.account_contacts + 1
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 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;
I am trying to run this query bug it keeps failing; can't use table in the from clause.
update student s1 set tot_cred = (select total_cred from student s inner join taken t on s.id=t.id inner join transfer_course tc on (t.transfer_course_id, t.college_id) = (tc.transfer_course_id, tc.college_id));
Any pointers appreciated!
Thanks
Try this instead:
I don't think the 'on' keyword allows multiple columns to be compared.
If it does it is news to me
update student s1
set tot_cred = (select total_cred
from student s
inner join taken t
on s.id=t.id
inner join transfer_course tc
on t.transfer_course_id = tc.transfer_course_id
and t.college_id = tc.college_id)
update student s1
set s1.tot_cred =
(select total_cred
from student s
inner join taken t
on s.id=t.id
inner join transfer_course tc
on (t.transfer_course_id = tc.transfer_course_id)
AND (t.college_id = tc.college_id)
);