这是我的想法......
我正在从数据库中进行选择,其中一列是oe.prodplan_id
。这个包含一个引用另一个表的数字。许多选定的行在oe.prodplan_id
中包含相同的数字,所以我只想要包含最低值的所有行。例如,oe.prodplan_id = 1
所有行。
问题是我现在不是最低值,所以从选定的行我只想要数量最少的那些..
也许我的解释很糟糕,但我的英语也是如此:)
这是我的SQL问题,如果它有帮助:
SELECT
oe.id AS oeID, oh.ordernumber, oe.order_line, oe.prod_week,
oe.prodplan_id, oe.door_type, oe.amount AS amountOfDoors, oe.inner_color,
oe.outer_color, oe.prod_week, a.name, a.description, i.amount AS iAmount,
md.status AS mdStatus, i.width, i.length, c.color_name AS innerColor,
c2.color_name AS outerColor, pp.status, oe.pos, d.name, md.id AS mdid
FROM pd_mounting_details AS md
LEFT OUTER JOIN pd_order_eco AS oe ON md.order_data = oe.id
LEFT OUTER JOIN pd_article AS a ON md.article = a.id
LEFT OUTER JOIN pd_inventory AS i ON md.id = i.pp_id
LEFT OUTER JOIN pd_order_head AS oh ON oe.order_head = oh.id
LEFT OUTER JOIN pd_colors AS c ON oe.inner_color = c.id
LEFT OUTER JOIN pd_colors AS c2 ON oe.outer_color = c2.id
LEFT OUTER JOIN pd_doors as d ON oe.door=d.id
LEFT OUTER JOIN pd_production_plan AS pp ON oe.prodplan_id = pp.id
WHERE
(a.production_group = 4)
AND (NOT (oe.amount = 0))
AND (pp.status = 4)
AND md.status = 4
AND (startTime = '' OR startTime IS NULL)
AND (stopTime = '' OR stopTime IS NULL)
ORDER BY oe.pos
祝你好运
答案 0 :(得分:2)
SELECT
oe.id AS oeID, oh.ordernumber, oe.order_line, oe.prod_week,
oe.prodplan_id, oe.door_type, oe.amount AS amountOfDoors, oe.inner_color,
oe.outer_color, oe.prod_week, a.name, a.description, i.amount AS iAmount,
md.status AS mdStatus, i.width, i.length, c.color_name AS innerColor,
c2.color_name AS outerColor, pp.status, oe.pos, d.name, md.id AS mdid
FROM pd_mounting_details AS md
LEFT OUTER JOIN pd_order_eco AS oe ON md.order_data = oe.id
LEFT OUTER JOIN pd_article AS a ON md.article = a.id
LEFT OUTER JOIN pd_inventory AS i ON md.id = i.pp_id
LEFT OUTER JOIN pd_order_head AS oh ON oe.order_head = oh.id
LEFT OUTER JOIN pd_colors AS c ON oe.inner_color = c.id
LEFT OUTER JOIN pd_colors AS c2 ON oe.outer_color = c2.id
LEFT OUTER JOIN pd_doors as d ON oe.door=d.id
LEFT OUTER JOIN pd_production_plan AS pp ON oe.prodplan_id = pp.id
WHERE
(a.production_group = 4)
AND (NOT (oe.amount = 0))
AND (pp.status = 4)
AND md.status = 4
AND (startTime = '' OR startTime IS NULL)
AND (stopTime = '' OR stopTime IS NULL)
--ADDED
AND oe.prodplan_id = (SELECT MIN(prodplan_id) FROM pd_order_eco)
ORDER BY oe.pos
答案 1 :(得分:1)
您使用MIN()函数。因为这是一个“聚合”函数,所以必须在GROUP BY子句中包含其他字段。 例如:
SELECT
MIN(prodplan.id),
oh.ordernumber
FROM
...
WHERE
...
GROUP BY
oh.ordernumber
原始结果集(基于所有表连接,过滤器等):
prodplan.id | oh.ordernumber | funny_word
1 | 1 | cow
2 | 1 | cow
3 | 1 | tree
聚合结果集(在prodplan.id上使用MIN)
1 | 1 | cow
3 | 1 | tree
它过滤掉了这条记录(因为除了prodplan.id之外的所有东西都是一样的,并且它保存的记录的prodplan.id是两条记录的最小数量)
2 | 1 | cow