Ok so what I need to do here is, take the sum of all the data with a specific name in the first table.
SELECT sum(DKP_Change) FROM 'Attendance' WHERE Name='harrian'
then in a DIFFERENT table I need to update the Total_DKP with the sum of the previous table
SELECT Total_DKP FROM `Characters` WHERE Name='harrian'
I tried the following solution and a few others but I'm not getting any working results
SELECT Total_DKP FROM `Characters` WHERE Name='harrian'
set Total_DKP = (SELECT sum(DKP_Change) FROM 'Attendance' WHERE Name='harrian')
To change data in a row, use the UPDATE statement:
UPDATE Characters AS c
SET c.Total_DKP = ( SELECT SUM(a.DKP_Change)
FROM Attendance AS a
WHERE a.Name = 'harrian'
)
WHERE c.Name = 'harrian' ;
UPDATE `Characters`
SET Total_DKP = (SELECT SUM(DKP_Change) FROM `Attendance` WHERE Name='harrian')
Try the following please.
update t1 set t1.secondcolumn =
(SELECT sum(blah) as blahsum
FROM t1 b
where b.name = 'harran'
)
WHERE t1.name = 'harran'
;
Related
I'm trying to update a column of a table so that is equal to the count of something in another table. Like this:
UPDATE TABLE
SET TOTAL = (SELECT COUNT(f1)
FROM TABLE2
GROUP BY f2);
But I keep getting sub query returns more than 1 row, and I can't think of how to fix it.
UPDATE (copied from the comment)
f2 is the relation between TABLE and TABLE2 – Thomasd d
Based on your comment
f2 is the relation between TABLE and TABLE2
you probably want something like this
UPDATE TABLE T1, (SELECT f2, COUNT(F1) cnt FROM TABLE2 GROUP BY f2) T2
SET T1.TOTAL = T2.cnt
WHERE T1.f2=T2.f2
adapt T1.f2 if necessary
UPDATE t1
SET total = ( SELECT COUNT(f1)
FROM t2
WHERE t1.f2 = t2.f2 );
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=91de17deff657f66fa54b42fe20ed3c5
Add WHERE total IS NULL if you do not need to recalculate values for rows which have a value already.
Your subquery is returning multiple values and your SET statement is only expecting one. This might fix your code if that is what you are looking for.
UPDATE TABLE
SET TOTAL = (SELECT COUNT(f1)
FROM TABLE2)
I need to update a list of records coming from a SELECT statement I'm trying with something of this kind but I got errors regarding the statement format:
UPDATE
noleggio_veicoli
SET
data_esportazione = CURDATE()
WHERE id IN
(SELECT id FROM noleggio_veicoli WHERE id_convenzionato = 3);
But it just returns:
Error Code: 1093. You can't specify target table 'noleggio_veicoli'
for update in FROM clause
It looks like MySQL is only able to run classic UPDATE i.e.:
UPDATE
noleggio_veicoli
SET
data_esportazione = CURDATE()
WHERE
id_convenzionato = 3;
Unlucky this's not what I need since SELECT query I need to chain to the UPDATE statement is more complex and depends on Views already defined in the SQL server.
Like #Maxim said, you should do something like
CREATE TEMPORARY TABLE IF NOT EXISTS temp AS (
SELECT id FROM noleggio_veicoli where id_convenzionato=3
);
update noleggio_veicoli set data_esportazione=curdate()
where id in (select id from temp);
If you want to update data that match a select p.id from parent p inner join child c on c.pid=p.id left join child2 c2 on c.x=c2.y where c.xx=yy and p.x=y... you can do something like:
update parent p
set aaa=bbbb
from child c
left join child2 c2 on c.x=c2.y
where c.pid=p.id and c.xx=yy and p.x=y ...
You can specify this with a join:
UPDATE noleggio_veicoli nv JOIN
noleggio_veicoli nv3
ON nv.id = nv3.id and nvid3.id_convenzionato = 3
SET data_esportazione = curdate();
For some odd reason MySql doesn't allow to reference the same table you update in a first level subquery.
But you can work around that via a subqueryinception. (It's not a real word, probably)
I.e. using a sub-query in the sub-query.
Example using an IN:
UPDATE noleggio_veicoli
SET data_esportazione = current_date
WHERE id IN (select id from (select id from noleggio_veicoli where id_convenzionato = 3) q);
It works also with an EXISTS:
UPDATE noleggio_veicoli as t
SET data_esportazione = current_date
WHERE EXISTS
(
select 1
from (select id, id_convenzionato from noleggio_veicoli) as t2
where t2.id_convenzionato = 2
and t2.id = t.id
);
You can test it here on rextester
You can't update and read from one table at the same time. At first you must
select necessary ids and save them to somewhere, for example to temporary table
I want to insert into the last column (number of people in that room) and
I want to use
insert into table(n_people_in_room)
select count(people_id)
from table
group by room
This is obvious wrong because i need to join the table with itself on people_id but i didn't. What is the right code?
Here's one way to do it, using an inline view to get the N_People_In_Room totals:
I'd do it as a SELECT first:
SELECT t.peopleid
, t.room
, t.n_people_in_room AS `old_npir`
, s.n_people_in_room AS `new_npir`
FROM mytable t
JOIN ( SELECT c.room
, COUNT(1) AS n_people_in_room
FROM mytable c
GROUP BY c.room
) s
ON s.room = t.room
Convert that into an UPDATE by repacing SELECT ... FROM with UPDATE, and adding a SET clause...
UPDATE mytable t
JOIN ( SELECT c.room
, COUNT(1) AS n_people_in_room
FROM mytable c
GROUP BY c.room
) s
ON s.room = t.room
SET t.n_people_in_room = s.n_people_in_room
I'm trying to update and select the same table as the following:
UPDATE tbl_domain SET remain =
(SELECT DATEDIFF(expdate,NOW()) FROM (
SELECT DATEDIFF(expdate,NOW()) FROM tbl_domain
) as x )
How can i make it work ? Someone please help me!!!
Try this:
UPDATE tbl_domain t, (SELECT DATEDIFF(expdate,NOW()) as remain
FROM tbl_domain
) t1
SET t.remain = t1.remain
-- WHERE t.Id = t1.Id --If you have some kind of unique id in your table.
I can't do this in MySQL
UPDATE tableA, tableB
SET tableA.column1 = SUM(tableB.column2)
WHERE tableA.column3 = tableB.column4
GROUP BY tableB.column4
;
Neither can I
UPDATE tableA,
(
SELECT SUM(tableB.column2) sumB, tableB.column4
FROM tableB
GROUP BY tableB.column4
) t1
SET tableA.column1 = sumB
WHERE tableA.column3 = column4
;
Besides it being illegal code, I think you can understand what I tried to do with the queries above. Both of them had the same intent.
How can I do that in MySQL?
This would be one way, if you don't mind using a subquery:
UPDATE tableA
SET column1 = (
SELECT sum(column2)
FROM tableB
WHERE tableA.coumn3 = tableB.column4);