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
Related
I'm trying to perform a simple update in SQL between 2 tables from different DB's. The challenge is that in order for the value to be updated it must meet certain conditions. I have used the join statements to meet the conditions and when I go to test the value from table B it is not being updated into table A. Here is what I've done so far.
USE [dbo]
GO
CREATE PROCEDURE
(
#User_ID = INT,
#Batch_ID VARCHAR(32)
)
DECLARE #locid int
SELECT #locid
FROM OtherDB.dbo.User AS UL
WHERE UL.User_ID = #User_Id
and User_Type = 1;
UPDATE M
SET
M.Number = W.Number
FROM dbo.tableA AS W
JOIN dbo.tableB AS B ON B.ID = W.ID
JOIN dbo.tableC AS C ON C.ToolA = B.ToolA
JOIN dbo.tableD as D ON D.Zone = W.Zone_Name
JOIN OtherDB.dbo.tableMax AS M ON M.LID = #locid
AND M.Tool = C.Other_Tool
AND M.Zone = D._Other_Zone
AND M.Station = W.Station
WHERE W.User_ID = #User_ID
AND W.Batch_ID = #Batch_ID
SET NOCOUNT OFF;
The update statement is updating table M in otherDB, not table A on the current database.
You might revise the stored procedure to be
Update A
Set A.Number = W.Number
From dbo.tableA A
....
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;
Can anyone see any reason why the second result set here produces 0 results even though if I run the queries independently the second one will return results??
SELECT GROUP_CONCAT(DISTINCT(mu.ID)) AS users
FROM `companies` c
JOIN `companysites` cs ON c.ID = cs.companyID
JOIN `users` mu ON cs.ID = mu.siteID
JOIN `targetcategories` tcat ON c.smTargetCategory = tcat.ID
WHERE isVendor = 0;
SET #in = "users";
SELECT *
FROM privileges
WHERE userID IN (#in);
I actually need the 2nd query to be a delete query but want to check results before I do it.
Pass your first query in a sub-query, for delete:
DELETE FROM privileges
WHERE userID IN (
SELECT GROUP_CONCAT(DISTINCT(mu.ID)) AS userID
FROM `companies` c
JOIN `companysites` cs ON c.ID = cs.companyID
JOIN `users` mu ON cs.ID = mu.siteID
JOIN `targetcategories` tcat ON c.smTargetCategory = tcat.ID
WHERE isVendor = 0
);
If mysql return error, you can try to 'encapsulate' your subquery:
DELETE p.*
FROM privileges p
WHERE userID IN (
SELECT userID
FROM
(SELECT GROUP_CONCAT(DISTINCT(mu.ID)) AS userID
FROM `companies` c
JOIN `companysites` cs ON c.ID = cs.companyID
JOIN `users` mu ON cs.ID = mu.siteID
JOIN `targetcategories` tcat ON c.smTargetCategory = tcat.ID
WHERE isVendor = 0) x);
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
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)
);