当连接条件在其他表中时 MySQL 排除连接

发布于 2024-12-16 23:58:33 字数 1884 浏览 2 评论 0原文

我正在尝试订购分组结果集。问题是其中一个连接条件引用了另一个表。具体来说,我需要从 table_a 中选择组内具有最高值的记录,但组 id 是另一个表中的字段。

以下是我尝试过的一些不同的变化。每当我添加 GROUP BY pr.id 时,一些已知应该位于前 3 名的结果被排除。当我不添加它时,每组会得到超过 1 条记录。

非常感谢任何帮助。

SELECT * FROM ivalues AS iv

INNER JOIN mprod AS p ON (p.id = iv.p_id)
INNER JOIN contact AS pr ON (pr.id = p.pr_id)


LEFT OUTER JOIN ivalues i2
    ON i2.p_id = p2.id

    AND i2.period = iv.period
    AND i2.current = iv.current
    AND i2.cert = iv.cert
    AND i2.exists = iv.exists
    AND iv.value > i2.value
WHERE 
iv.period = 0
AND iv.current = 1
AND iv.cert = 1
AND p.type_id = 15747
AND iv.exists = 1
AND i2.id IS NULL
GROUP BY pr.id  
ORDER BY iv.value ASC
LIMIT 10;

SELECT * FROM ivalues AS iv

INNER JOIN mprod AS p ON (p.id = iv.p_id)
INNER JOIN contact AS pr ON (pr.id = p.pr_id)

LEFT OUTER JOIN contact pr2 
    ON pr2.id = p.pr_id

LEFT OUTER JOIN mprod p2
    ON p2.type_id = p.type_id           

LEFT OUTER JOIN ivalues i2
    ON i2.p_id = p2.id

    AND i2.period = iv.period
    AND i2.current = iv.current
    AND i2.cert = iv.cert
    AND i2.exists = iv.exists
    AND iv.value > i2.value
WHERE 
iv.period = 0
AND iv.current = 1
AND iv.cert = 1
AND p.type_id = 15747
AND iv.exists = 1
AND i2.id IS NULL

ORDER BY iv.value ASC
LIMIT 10;

SELECT * FROM ivalues AS iv

INNER JOIN mprod AS p ON (p.id = iv.p_id)
INNER JOIN contact AS pr ON (pr.id = p.pr_id)

LEFT OUTER JOIN contact p2 
    ON p2.pr_id = p.pr_id   
    AND p2.type_id = p.type_id

INNER JOIN mprod p2
    ON p2.type_id = p.type_id           

INNER JOIN ivalues i2
    ON i2.p_id = p2.id

    AND i2.period = iv.period
    AND i2.current = iv.current
    AND i2.cert = iv.cert
    AND i2.exists = iv.exists
    AND iv.value > i2.value

WHERE 
iv.period = 0
AND iv.current = 1
AND iv.cert = 1
AND p.type_id = 15747
AND iv.exists = 1

ORDER BY iv.value ASC
LIMIT 3;

I'm trying to order a grouped resultset. Problem is that one of the join conditions references another table. Specifically, I need to select records from table_a that have the highest value within a group, but the group id is a field in a different table.

Below are some of the things I tried, in different variations. Whenever I add GROUP BY pr.id, some results that know should be in the top 3, are excluded. When I do not add it I get more than 1 record per group.

Any help much appreciated.

SELECT * FROM ivalues AS iv

INNER JOIN mprod AS p ON (p.id = iv.p_id)
INNER JOIN contact AS pr ON (pr.id = p.pr_id)


LEFT OUTER JOIN ivalues i2
    ON i2.p_id = p2.id

    AND i2.period = iv.period
    AND i2.current = iv.current
    AND i2.cert = iv.cert
    AND i2.exists = iv.exists
    AND iv.value > i2.value
WHERE 
iv.period = 0
AND iv.current = 1
AND iv.cert = 1
AND p.type_id = 15747
AND iv.exists = 1
AND i2.id IS NULL
GROUP BY pr.id  
ORDER BY iv.value ASC
LIMIT 10;

SELECT * FROM ivalues AS iv

INNER JOIN mprod AS p ON (p.id = iv.p_id)
INNER JOIN contact AS pr ON (pr.id = p.pr_id)

LEFT OUTER JOIN contact pr2 
    ON pr2.id = p.pr_id

LEFT OUTER JOIN mprod p2
    ON p2.type_id = p.type_id           

LEFT OUTER JOIN ivalues i2
    ON i2.p_id = p2.id

    AND i2.period = iv.period
    AND i2.current = iv.current
    AND i2.cert = iv.cert
    AND i2.exists = iv.exists
    AND iv.value > i2.value
WHERE 
iv.period = 0
AND iv.current = 1
AND iv.cert = 1
AND p.type_id = 15747
AND iv.exists = 1
AND i2.id IS NULL

ORDER BY iv.value ASC
LIMIT 10;

SELECT * FROM ivalues AS iv

INNER JOIN mprod AS p ON (p.id = iv.p_id)
INNER JOIN contact AS pr ON (pr.id = p.pr_id)

LEFT OUTER JOIN contact p2 
    ON p2.pr_id = p.pr_id   
    AND p2.type_id = p.type_id

INNER JOIN mprod p2
    ON p2.type_id = p.type_id           

INNER JOIN ivalues i2
    ON i2.p_id = p2.id

    AND i2.period = iv.period
    AND i2.current = iv.current
    AND i2.cert = iv.cert
    AND i2.exists = iv.exists
    AND iv.value > i2.value

WHERE 
iv.period = 0
AND iv.current = 1
AND iv.cert = 1
AND p.type_id = 15747
AND iv.exists = 1

ORDER BY iv.value ASC
LIMIT 3;

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

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

发布评论

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

评论(1

迷鸟归林 2024-12-23 23:58:33

我认为您需要将 ivalues 和 mprod 作为子选择连接,或者作为外部连接来排除非最大值,或者作为内部组来查找最大值。希望它看起来像这样:

SELECT * FROM contact
  INNER JOIN mprod ON mprod.contact_id = contact.id
  INNER JOIN ivalues ON ivalues.mprod_id = mprod.id
  LEFT OUTER JOIN (
    SELECT mprod.contact_id, ivalues.*
      FROM mprod
      INNER JOIN ivalues ON mprod.id = ivalues.mprod_id
      WHERE ivalues.period = 0
        AND ivalues.current = 1
        AND ivalues.cert = 1
        AND mprod.type_id = 15747
        AND ivalues.exists = 1
  ) AS pv ON pv.contact_id = contact.id AND pv.value > ivalues.value
  WHERE ivalues.period = 0
    AND ivalues.current = 1
    AND ivalues.cert = 1
    AND mprod.type_id = 15747
    AND ivalues.exists = 1
    AND pv.id IS NULL
  GROUP BY pr.id  
  ORDER BY iv.value ASC
  LIMIT 10;

I think you will need to join ivalues and mprod as a subselect, either as an outer join to exclude non-maximum values, or as an inner group to find the maximum value. Hopefully it looks something like this:

SELECT * FROM contact
  INNER JOIN mprod ON mprod.contact_id = contact.id
  INNER JOIN ivalues ON ivalues.mprod_id = mprod.id
  LEFT OUTER JOIN (
    SELECT mprod.contact_id, ivalues.*
      FROM mprod
      INNER JOIN ivalues ON mprod.id = ivalues.mprod_id
      WHERE ivalues.period = 0
        AND ivalues.current = 1
        AND ivalues.cert = 1
        AND mprod.type_id = 15747
        AND ivalues.exists = 1
  ) AS pv ON pv.contact_id = contact.id AND pv.value > ivalues.value
  WHERE ivalues.period = 0
    AND ivalues.current = 1
    AND ivalues.cert = 1
    AND mprod.type_id = 15747
    AND ivalues.exists = 1
    AND pv.id IS NULL
  GROUP BY pr.id  
  ORDER BY iv.value ASC
  LIMIT 10;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文