当连接条件在其他表中时 MySQL 排除连接
我正在尝试订购分组结果集。问题是其中一个连接条件引用了另一个表。具体来说,我需要从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要将 ivalues 和 mprod 作为子选择连接,或者作为外部连接来排除非最大值,或者作为内部组来查找最大值。希望它看起来像这样:
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: