Updating a table with value sum of another table in VBA - ms-access

I would like to update a sum(columne) from table1 to column from table2
I try to do this in sql like this:
UPDATE stock SET stock.chairout = (SELECT SUM(chairs_count) FROM Event
)
WHERE (([Event].[returned]))=False;
but it's giving me error like (it's not update able query)
can you guys help out on this?

Your query is:
UPDATE stock
SET stock.chairout =
(
SELECT SUM(chairs_count)
FROM Event
)
WHERE Event.returned = False
Shouldn't your where clause be on the inside of the parenthesis, like so:
UPDATE stock
SET stock.chairout =
(
SELECT SUM(chairs_count)
FROM Event
WHERE Event.returned = False
)

Related

MySQL - Using update/set with like from multiple tables

I am trying to update one column called 'Name' in one table using the naming conventions from another table.
My script below is not working and I am not quite sure why... Either get a syntax error or another way I tried to do this I ended up with all the 'Name' as NULL:
UPDATE table1
SET Name =
ISNULL(
(SELECT TOP 1 CorrectSSPname
FROM table2
WHERE UPPER(Name) LIKE '%’ + UPPER(WrongSSPname) + ‘%')
, Name
)
WHERE DATE >= '2018-07-01'
I can get 'like' to work using the following script for a single update but unable to do multi updates using the script above:
UPDATE table1
SET Name = 'xxx'
WHERE Name like 'yyy'
You can try a query like this:
UPDATE tabel1 tbl1
LEFT JOIN table2 tbl2 ON UPPER(tbl2.WrongSSPname) LIKE CONCAT('%', UPPER(tbl1.Name), '%')
SET tbl1.Name = tbl2.CorrectSSPname
WHERE tbl1.DATE >= '2018-07-01'

MySQL Conditional Update in same table

I'm looking for a simple way to do an update on a table only if there is no other columns present in that same table with the same value I'm trying to update, ideally in a single query. So far I'm getting an error You specify target table 't1' for update in FROM clause. Here is what I tried in a few variations so far (still unable to get working):
UPDATE emailQueue AS t1
SET
t1.lockedOn = 1470053240
WHERE
(SELECT
COUNT(*)
FROM
emailQueue AS t2
WHERE
t2.lockedOn = 1470053240) = 0
AND t1.lockedOn IS NULL
In MySQL, you need to use a join. In this case, a left join is in order:
UPDATE emailQueue eq LEFT JOIN
emailQueue eq2
ON eq2.lockedOn = 1470053240
SET eq.lockedOn = 1470053240
WHERE eq.lockedOn IS NULL AND
eq2.lockedOn IS NULL;

mySQL update a value

Modified some stuff from my pic so you guys can understand it
I have this database. I am trying to update a value from a table based on another value from an another table.
I want to update the SUM from salary like this :
( sum = presence * 5 )
This is what I've been trying to use ( unsuccessful )
update table salary
set suma.salary = users.presence * 5
FROM salary INNER JOIN users1 INNER JOIN presence on id_salary = id_presence
I am not sure what to do, I'd appreciate some help, Thanks
In MySQL to UPDATE tables with a join you use this syntax:
UPDATE table1, table2
SET table1.column = some expression
WHERE table1.column = table2.column
That said, even with the updated picture, in your SQL you are mentioning columns that I cannot understand in which table are to be found. You also have an inner join between salariu and users1, with no join condition. Could you please clean up the question and make everything clear?
Assuming you are making the updates to the db structure you were talking about, then you can start working on this one maybe:
UPDATE salary, presence
SET salary.sum = SUM(presence.hours) * 5
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
Another way, but I'm not sure it is supported in all RDBMS, would be something like this:
UPDATE salary
SET sum = (
SELECT SUM(presence.hours) * 5
FROM user, presence
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
)

Multi Subquery count function unable to take the stress of multiple selects

This has me stumped.
MySQL
UPDATE sets SET sets.current_count = (SELECT COUNT(leads_auto.set_id) AS current_count FROM leads_auto WHERE leads_auto.set_id = (SELECT sets.set_id AS setID FROM sets WHERE sets.on_off = 0)) WHERE sets.on_off = 0
Seems right doesn't it? Update the record current_count with the total number of rows in leads_auto which have the set_id value of the set_id of the sets table where the value of its on_off column is 0.
yet I get this error
#1093 - You can't specify target table 'sets' for update in FROM clause
I looked around and someone mentioned that it has to do with the operation being cyclic?
Create a temporary table for the result of SET
UPDATE sets
SET sets.current_count =
(
SELECT COUNT(leads_auto.set_id) AS current_count
FROM leads_auto
WHERE leads_auto.set_id =
(
SELECT set_id
FROM
(
SELECT sets.set_id AS setID
FROM sets
WHERE sets.on_off = 0
) c
)
)
WHERE sets.on_off = 0

pure MySQL loop to update multiple rows

I want to update multiple rows based on a SELECT sql query.
I want to do it ALL IN AN SQL SHELL!
Here is my select:
SELECT #myid := id, #mytitle := title
FROM event
WHERE pid>0 GROUP BY title
ORDER BY start;
Then, I want to do an update with this pseudocode:
foreach($mytitle as $t)
BEGIN
UPDATE event
SET pid=$myid
WHERE title=$t;
END
But I don't know how to ake a loop in SQL.
Maybe there's a way to make it in a single sql query?
I DON'T WANT ANY PHP!!! ONLY SQL SHELL CODE!!!
I want to update every rows with a pid with the id of the first occurence of an event. Start is a timestamp
I think this should do what you want, but if it doesn't (I'm not sure about joining a subquery in an UPDATE query) then you can use a temporary table instead.
UPDATE
event
JOIN (
SELECT
MIN(pid) AS minPID,
title
FROM
event
WHERE
pid > 0
GROUP BY
title
) AS findPIDsQuery ON event.title = findPIDsQuery.title
SET
event.pid = findPIDsQuery.minPID
Pure SQL doesn't really have "loops", per se: it's a set-based descriptive language. I believe the following update will do what you want (though your problem statements leaves much to be desired—we know nothing about the underlying schema).
update event t
set pid = ( select min(id)
from event x
where x.title = t.title
and x.pid > 0
group by x.title
having count(*) > 1
)
Cheers!