mySQL UPDATE table based on SELECT (count) of a different table
Tag : php , By : Ben Kohn
Date : March 29 2020, 07:55 AM
this one helps. I have a table of classes, and a table of subjects. , Edit: How about this:
UPDATE SUBJECT
LEFT JOIN (
SELECT count(*) AS num, subject_id
FROM class
GROUP BY subject_id
where date_time > NOW()) AS t ON SUBJECT.subject_id = t.subject_id
SET SUBJECT.current_class_count = coalesce( t.num, 0 )
|
Update entire table based on parameter of another table in a matching field
Date : March 29 2020, 07:55 AM
I hope this helps . I've my table user_data that has over 8k records. Then I have my table Geo_location. Both tables join on the member_num column , I don't know if I understand the problem, but isn't UPDATE Geo_location AS g
JOIN users_data AS u ON u.member_num = g.member_num
SET g.public = ud.acceptinClients
|
How do you update a sql table based on distinct matching counts in another table?
Date : March 29 2020, 07:55 AM
hop of those help? You can't group by in your update statement. You just need to form the query separately that will give you the rows you need to update and join on that. You are updating based on ID, and the value you are setting depends on the number of open events for that ID, so form a query finding the number of open events by id: -- query open event counts by contact id
SELECT ID, COUNT(*) AS OpenEventCount
FROM Events
WHERE EventType = 'Open'
GROUP BY ID
UPDATE c
SET c.Tier = CASE
WHEN COALESCE(ec.OpenEventCount, 0) > 2 then 'Max'
WHEN COALESCE(ec.OpenEventCount, 0) = 2 then 'High'
ELSE 'Low'
END
FROM Contacts c
LEFT OUTER JOIN ( -- left join to update contacts with no open events
SELECT ID, COUNT(*) AS OpenEventCount
FROM Events
WHERE EventType = 'Open'
GROUP BY ID
) ec ON ec.ID = c.ID
|
How to update table based on count of entries of another table
Tag : sql , By : John Phipps
Date : March 29 2020, 07:55 AM
may help you . I've table as below , You can do this using conditional aggregation: select source, action,
count(*) as total_received,
sum(case when status = 'Closed' then 1 else 0 end) as total_closed,
sum(case when status = 'Open' and DueDate < Today() then 1 else 0 end) as overdue
from t
group by source, action;
|
Update a Table Based on Count from Another Table
Date : March 29 2020, 07:55 AM
this will help I am trying to do a COUNT against a table using a date range, from another table. The count has to also match with a reference number, here's the data format: , You don't need the CASE statement, a simple WHERE will suffice: UPDATE [Insight].[dbo].[Rev Old New]
SET [Rev Old New].[Calls] = (SELECT COUNT(*) FROM [Insight].[dbo].[Calls] t1
WHERE t1.Date BETWEEN [Rev Old New].[Start] AND [Rev Old New].[Finish])
|