sql喜欢看起来不像我认为应该

发布于 2025-01-28 12:19:26 字数 1697 浏览 6 评论 0原文

我有一个精选的语句,剩下的加入大量,我想过滤一些。

当我检查总共有多少记录并用我的类似语句减去记录时,我应该得到不受限制影响的金额。

但是,当我否定自己的限制以获得我没有影响的限制时,我的数字与计算的数字不同。

SQL无限制(记录计数:13.251.981)

SELECT p.product_number
FROM product p
LEFT JOIN product_category pc on p.id = pc.product_id
LEFT JOIN product_category_tree pct on p.id = pct.product_id
LEFT JOIN product_configurator_setting pcs on p.id = pcs.product_id
LEFT JOIN product_cross_selling pcs2 on p.id = pcs2.product_id
LEFT JOIN product_cross_selling_assigned_products pcsap on p.id = pcsap.product_id
LEFT JOIN product_cross_selling_translation pcst on pcs2.id = pcst.product_cross_selling_id
LEFT JOIN product_custom_field_set pcfs on p.id = pcfs.product_id
LEFT JOIN product_media pm on p.id = pm.product_id
LEFT JOIN product_option po on p.id = po.product_id
LEFT JOIN product_price pp on p.id = pp.product_id
LEFT JOIN product_property pp2 on p.id = pp2.product_id
LEFT JOIN product_review pr on p.id = pr.product_id
LEFT JOIN product_search_keyword psk on p.id = psk.product_id
LEFT JOIN product_tag pt on p.id = pt.product_id
LEFT JOIN product_translation pt2 on p.id = pt2.product_id
LEFT JOIN product_visibility pv on p.id = pv.product_id

具有限制性(记录计数:9.285.545),

WHERE p.product_number NOT LIKE 'SW%'
  AND p.product_number NOT LIKE '%.%'
  AND pt2.name NOT LIKE '%Gutschein'
  AND pt2.name NOT LIKE '%Test%'

并从我的计算中被否定的限制(记录计数:100.851)

WHERE p.product_number LIKE 'SW%'
   OR p.product_number LIKE '%.%'
   OR pt2.name LIKE '%Gutschein'
   OR pt2.name LIKE '%Test%';

,我应该获得3.966.436记录,而不会受到影响。 (13.251.981-9.285.545 = 3.966.436)

但是我得到了100.851,

这是怎么可能的?

I have an SELECT statement that has a huge number of left join and I want to filter some out.

When I check how many records i have in total and subtract the records with my LIKE statements, I should get the amount that is not affected by my restrictions.

But when I negate my restriction to get the ones I didn't affect, I get an different number than calculated.

SQL without restrictions (Record count: 13.251.981)

SELECT p.product_number
FROM product p
LEFT JOIN product_category pc on p.id = pc.product_id
LEFT JOIN product_category_tree pct on p.id = pct.product_id
LEFT JOIN product_configurator_setting pcs on p.id = pcs.product_id
LEFT JOIN product_cross_selling pcs2 on p.id = pcs2.product_id
LEFT JOIN product_cross_selling_assigned_products pcsap on p.id = pcsap.product_id
LEFT JOIN product_cross_selling_translation pcst on pcs2.id = pcst.product_cross_selling_id
LEFT JOIN product_custom_field_set pcfs on p.id = pcfs.product_id
LEFT JOIN product_media pm on p.id = pm.product_id
LEFT JOIN product_option po on p.id = po.product_id
LEFT JOIN product_price pp on p.id = pp.product_id
LEFT JOIN product_property pp2 on p.id = pp2.product_id
LEFT JOIN product_review pr on p.id = pr.product_id
LEFT JOIN product_search_keyword psk on p.id = psk.product_id
LEFT JOIN product_tag pt on p.id = pt.product_id
LEFT JOIN product_translation pt2 on p.id = pt2.product_id
LEFT JOIN product_visibility pv on p.id = pv.product_id

With restriction (Record count: 9.285.545)

WHERE p.product_number NOT LIKE 'SW%'
  AND p.product_number NOT LIKE '%.%'
  AND pt2.name NOT LIKE '%Gutschein'
  AND pt2.name NOT LIKE '%Test%'

With negated restriction (Record count: 100.851)

WHERE p.product_number LIKE 'SW%'
   OR p.product_number LIKE '%.%'
   OR pt2.name LIKE '%Gutschein'
   OR pt2.name LIKE '%Test%';

From my calculations i should get 3.966.436 records that don't get affected. (13.251.981 - 9.285.545 = 3.966.436)

But instead I get 100.851

How is that possible?

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

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

发布评论

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

评论(1

快乐很简单 2025-02-04 12:19:26

对我来说,解决方案实际上是:
其中p.product_number< 'SW'

The solution for me was actually this WHERE:
WHERE p.product_number < 'SW'

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