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;
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 have an empty table (t1) and I want to insert or update the t1.uid column from another table's (t2) GROUP BY uid values.
So far I have tried like this:
UPDATE table1 t1 JOIN
(SELECT uid FROM table2 GROUP BY uid) t2
SET t1.uid = t2.uid;
but it's not working for me.
N.B. I've got a massive data set for which group by (uid from table-t2) results giving me total 1114732 results which I have to insert/update in t1 table's uid column.
Please try this:
Insert into table1(uid)
select distinct uid from table2
If table1 is empty, then UPDATE is not the correct verb. Would this suit your needs?
INSERT into table1 SELECT distinct uid from table2;
INSERT ... SELECT docs
I have one table name "sections_content" that has the following columns
and I want to get max id for each statecode and policyname
So ,for example,the result of statecode CN / IL /IE should be
How should I write the code for mysql ?
thanks.
I think you can solve this with a JOIN on the MAX for that group:
SELECT * FROM sections_content as t
JOIN
(
SELECT MAX(id) as id
FROM sections_content AS tbl
GROUP BY
tbl.policyName,
tbl.statecode
) AS maxId
ON maxId.id=t.id
I think you can use ORDER BY and LIMIT keywords to get the maximum number.
Exp: Having tables; table1 and table2 as in your database. check below queries.
SELECT table1.id, table2.statecode, table2.policyname, sections_content.student_lga_of_origin
FROM table1
JOIN table2
ON table1.policyname = table2.policyname
ORDER BY table1.id DESC
LIMIT 1
OFFSET 1
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
);
I know this sounds rather confusing but I'm at a loss how to explain it better. I have a table simplified below:
DB Type ID
================
Table1 1
Table1 2
Table1 3
Table1 4
Table1 5
Table2 6
Table2 7
Table2 8
Table2 9
Table2 10
what i am trying to achieve is to basically clean out this table but keep the record with the highest ID for each DB Type if that makes sense - so in this case it would be (Table1,5) and (Table2,10) with all other records being deleted. Is it possible to do this exclusively through MySQL?
*EDIT***
Answer thanks to tips from Yogendra Singh
DELETE FROM MyTable WHERE ID NOT IN (SELECT * FROM (SELECT MAX(ID) from MyTable GROUP BY DB Type) AS tb1 ) ORDER BY ID ASC
TRY selecting the max ID group by db_type first and then use it as sub query with not in.
DELETE FROM MyTable
WHERE ID NOT IN
(SELECT ID FROM
(SELECT MAX(ID) AS ID from MyTable GROUP BY DB Type) AS tb1
)
EDIT:
DELETE FROM MyTable
HAVING MAX(ID) > ID;
delete your_table
from
your_table left join
(select max(id) max_id from your_table group by type) mx
on your_table.id=mx.max_id
where mx.max_id is null
Subquery returns the maximum id for every type, and those are the values to keep. With an left join i'm selecting all the rows from your table that don't have an in in max_ids, and those are the rows to delete. This will work only if id is primary key, otherwise we have to join also the type.
Is the combination DB Type - ID unique?
If so, you can attack this in two stages:
Get only the rows you want
SELECT [DB Type], Max(ID) AS MaxID
FROM YourTable
GROUP BY [DB Type]
Delete the rest (Wrapping the previous statement into a more complicated statement; don't mean that)
DELETE FROM YourTable
FROM
YourTable
LEFT JOIN
(SELECT [DB Type], Max(ID) AS MaxID
FROM YourTable GROUP BY [DB Type]) DontDelete
ON
YourTable.[DB Type]=DontDelete.[DB Type] AND
YourTable.ID=DontDelete.MaxID
WHERE
DontDelete.[DB Type] IS NULL
DELETE FROM MyTable del
WHERE EXISTS (
(SELECT *
FROM MyTable xx
WHERE xx."db Type" = del."db Type"
AND xx.id > del.id
);
delete from my_Table
where Day in (select MAX(day) d from my_Table where id='id')