MySQL中计算多个表中的匹配值

发布于 2024-11-29 15:15:17 字数 292 浏览 2 评论 0原文

我有 6 个供应商“库存”数据库,我已将它们与主数据库匹配/不匹配。如果在库存中找到了主数据中的唯一 ID,我们会将“匹配”一词放入我们创建的名为“状态”的列中。如果主站没有匹配的唯一 ID,我们会在“状态”列中输入“不匹配”。我们已经对每个供应商库存数据库执行了此操作。我想对具有“匹配”和“不匹配”的唯一 ID 的数量进行连续计数,以便快速浏览。

例如:

组合所有 6 个库存数据库的结果:
库存总量:20000 “比赛”总数:14000 “不匹配”总数:6000

I have 6 vendor "inventory" databases that I have matched/not matched to a master database. If a unique ID was found in the inventory that was also in the master, we put the word 'Match' in a column we created called Status. If the master did not have a matching unique ID, we put 'No Match' in the Status column. We have done this for each vendor inventory database. I want to keep a running count of the amount of unique ID's that have 'Match' and 'No Match' to look at for a quick glance.

For example:

Results that combine all 6 inventory databases:
Inventory total: 20000
'Match' total: 14000
'No Match' total: 6000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼中杀气 2024-12-06 15:15:17

这可能不是最好的方法,也没有经过测试,但这就是我想出的方法。


SELECT SUM(1) AS inventory_total, 
   SUM(CASE status WHEN 'Match' THEN 1 ELSE 0 END) AS match_total,
   SUM(CASE status WHEN 'No Match' THEN 1 ELSE 0 END) AS no_match_total
FROM tbl
WHERE tbl_id IN (
    SELECT MAX(tbl_id)
    FROM tbl
    GROUP BY fkey_id
);

我不完全确定这是否是您所追求的,但我希望它有所帮助。

This may not be the best way, nor is it tested, but here's what I came up with.


SELECT SUM(1) AS inventory_total, 
   SUM(CASE status WHEN 'Match' THEN 1 ELSE 0 END) AS match_total,
   SUM(CASE status WHEN 'No Match' THEN 1 ELSE 0 END) AS no_match_total
FROM tbl
WHERE tbl_id IN (
    SELECT MAX(tbl_id)
    FROM tbl
    GROUP BY fkey_id
);

I'm not entirely sure if that's what you're after or not, but i hope it helps.

天涯沦落人 2024-12-06 15:15:17

这行得通吗?

SELECT 
    COUNT(*) AS InventoryTotal,
    SUM(CASE WHEN STATUS = 'Match' THEN 1 ELSE 0 END) AS MatchTotal,
    SUM(CASE WHEN STATUS = 'No Match' THEN 1 ELSE 0 END) AS NoMatchTotal
FROM
    Table

Would this work?

SELECT 
    COUNT(*) AS InventoryTotal,
    SUM(CASE WHEN STATUS = 'Match' THEN 1 ELSE 0 END) AS MatchTotal,
    SUM(CASE WHEN STATUS = 'No Match' THEN 1 ELSE 0 END) AS NoMatchTotal
FROM
    Table
自此以后,行同陌路 2024-12-06 15:15:17
SELECT COUNT(*) as [inventory total],
    SUM(CASE status WHEN 'Match' THEN 1 ELSE 0 END) AS [match total]
    SUM(CASE status WHEN 'No Match' THEN 1 ELSE 0 END) AS [no match total]
FROM db1.table UNION ALL db2.table UNION ALL db3.table
               UNION ALL db4.table UNION ALL db5.table UNION ALL db6.table

如果我理解正确的话...

SELECT COUNT(*) as [inventory total],
    SUM(CASE status WHEN 'Match' THEN 1 ELSE 0 END) AS [match total]
    SUM(CASE status WHEN 'No Match' THEN 1 ELSE 0 END) AS [no match total]
FROM db1.table UNION ALL db2.table UNION ALL db3.table
               UNION ALL db4.table UNION ALL db5.table UNION ALL db6.table

If I understand you correctly...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文