I'm getting an error when trying to run this query:
UPDATE tbl1, tbl2 SET `tb1`.field=SUM(tbl2.grade) WHERE tbl2.word=tbl1.word
This is the error: #1111 - Invalid use of group function
I basically trying to get the sum of a field from tbl2 and insert it into tbl1
Join against a subquery that calculates the total grade for every word.
UPDATE tbl1 a
INNER JOIN
(
SELECT word, SUM(grade) totalGrade
FROM tbl2
GROUP BY word
) b ON a.word = b.word
SET a.field = b.totalGrade
The easiest way to achieve what you want to do is via a subquery:
UPDATE tbl1 SET tbl1.field=
(SELECT SUM(tbl2.grade) FROM tbl2 WHERE tbl2.word=tbl1.word)
You're selecting multiple values from tbl2 and want the sum of them for each entry in tbl1.
Try doing:
UPDATE tbl1
SET `tbl1`.field = (
SELECT SUM(tbl2.grade)
FROM tbl2
WHERE tbl2.word = tbl1.word
);
Related
Thanks to this question Rename Mysql Duplicate Value I was able to come up with this queryn to elminate the duplicate rows.
UPDATE table1
inner join (SELECT OBJECTID,CONCAT(IDENT,'_1') as IDENT FROM table1
GROUP BY IDENT HAVING COUNT(*) > 1) t
on t.OBJECTID = table1.OBJECTID
SET table1.IDENT = t.IDENT;
This works well but I want to only rename the rows where the column IDENT is duplicated and the NAME column is different. Any ideas how to do this?
Change the grouping to be both NAME and IDENT.
UPDATE table1
JOIN (
SELECT MAX(objectid) AS max_id, name, CONCAT(ident, '_1') AS new_ident
FROM table1
GROUP BY name, ident
HAVING COUNT(*) > 1
) AS t ON t.max_id = table1.objectid
SET table1.ident = t.new_ident
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)
DISTINCT in mysql query removing the records from resultset
I have three tables
TBL1 TBL2 TBL3
---- ------ --------
tbl1_id tbl2_id tbl3_id
cid fkcid fkcid
fktbl1_id fktbl2_id
I have query to get records of TBL3
select distinct tbl3.* from TBL3 tbl3
inner join TBL2 tbl2 on tbl2.tbl2_id = tbl3.fktbl2_id and tbl2.fkcid = tbl3.fkcid
inner join TBL1 tbl1 on tbl1.tbl1_id = tbl2.fktbl1_id and tbl2.fkcid = tbl1.cid;
This query gives me around 1000 records.
But when I removes distinct from query it gives me around 1100 records.
There is no duplicate records in table.Also I confirmed that these extra 100 are not duplicate.Please note That these extra 100 records are not found in query with distinct keyword.
Why this query is behaving unexpectedly.Please help me to understand more clearly and correct me if i am making mistake.
Thank you
You have multiple records in tbl1 or tbl2 that map to the same tbl3, and since you're only selecting tbl3.* in your output, DISTINCT removes the duplication. To instead find what the duplicates are, remove the DISTINCT, add a COUNT(*) to the SELECT clause, and add at the end a GROUP BY and HAVING, such as:
select tbl3.*, count(*)
from TBL3 tbl3
inner join TBL2 tbl2 on tbl2.tbl2_id = tbl3.fktbl2_id and tbl2.fkcid = tbl3.fkcid
inner join TBL1 tbl1 on tbl1.tbl1_id = tbl2.fktbl1_id and tbl2.fkcid = tbl1.cid
group by tbl3.tbl3_id, tbl3.fkcid, tbl3.fktbl2_id having count(*) > 1;
I wanna run a subquery that uses the value of the outer query in its where clause. Here's and example of what I wanna do:
SELECT * FROM `tbl1`
WHERE `tbl1`.`max_count` < (
SELECT COUNT(*) rc FROM `tbl2`
WHERE `tbl2`.`id` = `tbl1`.`id
)
There is tbl1 with a column named max_count, and there is tbl2 with rows referring to a row in tbl1(many-to-one relationship). What I wanna do is select rows in tbl1 where the number of rows in tbl2 referencing it is less than the max_count value of that row. But I'm pretty sure that what I wrote here, ain't gonna cut it. Any ideas?
Thanks a lot
try this -
SELECT * FROM `tbl1` t1
WHERE t1.`max_count` < (
SELECT COUNT(*) FROM `tbl2` t2
WHERE t2.`id` = t1.`id`
)
try using JOIN.
SELECT DISTINCT a.*
FROM tb1 a
INNER JOIN
(
SELECT id, COUNT(*) totalCount
FROM tbl2
GROUP BY id
) b ON a.ID = b.ID
WHERE a.max_count < b.totalCount
As an alternate solution, it's probably easier to just use a LEFT JOIN with HAVING than a subquery;
SELECT tbl1.*, COUNT(tbl2.id) current_count
FROM tbl1
LEFT JOIN tbl2
ON tbl1.id=tbl2.id
GROUP BY tbl1.id
HAVING COUNT(tbl2.id) < max_count
An SQLfiddle to test with.
Note that the GROUP BY in this case is a MySQL only thing, normally you'd need to GROUP BY every selected field in tbl1 even if tbl1.id is known to be unique per row.
I thought this would be simple but I can't get my head around it...
I have one table tbl1 and it has columns id,otherstuff,num.
I have another table tbl2 and it has columns id,info.
What I want to is make the num column of tbl1 equal to the number of rows with the same id in tbl2. Kind of like this:
UPDATE tbl1 SET num =
(SELECT COUNT(*) FROM tbl2 WHERE id=tbl1.id)
Any ideas?
If your num column is a valid numeric type your query should work as is:
UPDATE tbl1 SET num = (SELECT COUNT(*) FROM tbl2 WHERE id=tbl1.id)
UPDATE tbl1, (select id, count(*) as idCount from tbl2 group by id) as t2
SET tbl1.num = t2.idCount
WHERE tbl1.id = t2.id;