简体   繁体   English

通过插入另一个表中的外键计数来更新表

[英]Update a table by inserting a count of foreign key from another table

I have two tables: 我有两个表:

    ╔════════════════╗    ╔════════════════╗
    ║ ITEM           ║    ║ ITEM_TRACK     ║
    ╠════════════════╣    ╠════════════════╣
    ║ ID             ║    ║ ID             ║
    ║ GUID           ║    ║ ITEM_GUID      ║
    ║ COUNT1         ║    ║ CONTEXT        ║
    ║ ENDDATE        ║    ║                ║
    ╚════════════════╝    ╚════════════════╝

╔═════╦══════╦════════╗   ╔═════╦═══════════╦══════════╗
║ ID  ║ GUID ║ COUNT1 ║   ║ ID  ║ ITEM_GUID ║ CONTEXT  ║
╠═════╬══════╬════════╣   ╠═════╬═══════════╬══════════╣
║ 1   ║  aaa ║        ║   ║ 1   ║    abc    ║   ITEM   ║
║ 2   ║  bbb ║        ║   ║ 2   ║    aaa    ║   PAGE   ║
║ 3   ║  ccc ║        ║   ║ 3   ║    bbb    ║   ITEM   ║
║ 4   ║  abc ║        ║   ║ 4   ║    ccc    ║   ITEM   ║
╚═════╩══════╩════════╝   ║ 5   ║    abc    ║   ITEM   ║
                          ║ 6   ║    aaa    ║   ITEM   ║
                          ║ 7   ║    abc    ║   ITEM   ║
                          ║ 8   ║    ccc    ║   PAGE   ║
                          ╚═════╩═══════════╩══════════╝

What I'm trying to do is fill in the COUNT1 column in ITEM with the count of the number of times ITEM_GUID appears in ITEM_TRACK for all ITEM.GUIDs where ENDDATE is still in the future. 我想做的是在ITEM的COUNT1列中填写ITEM_GUID在将来仍在ENDDATE的所有ITEM.GUID中出现在ITEM_TRACK中的次数。 I need to do this once an hour for all GUIDS in ITEM. 我需要每小时对ITEM中的所有GUIDS执行一次。

I can get the counts I need easily 我可以轻松获得所需的计数

SELECT ITEM_GUID, COUNT(*) from ITEM_TRACK GROUP BY ITEM_GUID;

What I don't know how to do is, how do I merge this with an INSERT INTO statement to automatically update all the items in the items table with the count based on their ENDDATE? 我不知道该怎么办,如何将其与INSERT INTO语句合并,以自动根据其ENDDATE的计数更新项目表中的所有项目?

UPDATE: I have a working solution based on Aquillo's answer: 更新:我有一个基于Aquillo答案的有效解决方案:

UPDATE ITEM a
SET COUNT1 = (SELECT COUNT(*) AS total FROM ITEM_TRACK b WHERE b.item_guid=a.guid);

Is there any other way to do this without a subquery? 没有子查询还有其他方法吗?

You can insert from a select like this: 您可以从这样的选择中插入:

INSERT INTO myTable (foreignKey, countColumn) VALUES 
    SELECT ITEM_GUID, COUNT(*) from ITEM_TRACK GROUP BY ITEM_GUID;

In case you want to update, try something like this: 如果要更新,请尝试以下操作:

UPDATE from SELECT using SQL Server 使用SQL Server从SELECT更新

If you use INSERT INTO you'll put additional rows in your ITEM table, not update the existing ones. 如果您使用INSERT INTO,则会在ITEM表中放置其他行,而不更新现有行。 If this is what you meant then that's great, but if you want to update the existing ones, you'll need to use update. 如果这是您的意思,那很好,但是如果您想更新现有的,则需要使用update。 You do this by joining the table you want to update with the table you want to update from. 为此,您可以将要更新的表与要更新的表连接在一起。 However, in your case you want to update from an aggregation and so you need to create a table with the aggregated values. 但是,在您的情况下,您想从聚合中进行更新,因此您需要创建一个具有聚合值的表。 Try this: 尝试这个:

UPDATE ITEM SET Count1 = temp.total
FROM Item
INNER JOIN (
    SELECT ITEM_GUID, COUNT(*) AS total
    FROM ITEM_TRACK
    GROUP BY ID) AS temp
ON Item.GUID = temp.ITEM_GUID
WHERE ENDDATE > NOW()

I've tried this on SQL Server (using GETDATE() instead of NOW()) to double check and it worked, I think it should work on MYSQL. 我已经在SQL Server(使用GETDATE()而不是NOW())上进行了仔细检查,并且可以正常工作,我认为它应该可以在MYSQL上工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM