SELECT
bp.product_id,bs.step_number,
p.price, pd.name as product_name
FROM
builder_product bp
JOIN builder_step bs ON bp.builder_step_id = bs.builder_step_id
JOIN builder b ON bp.builder_id = b.builder_id
JOIN product p ON p.product_id = bp.product_id
JOIN product_description pd ON p.product_id = pd.product_id
WHERE b.builder_id = '74' and bs.optional != '1'
group by bs.step_number
ORDER by bs.step_number, p.price
但这是我的结果
88 1 575.0000 Lenovo Thinkcentre POS PC
244 2 559.0000 Touchscreen with MSR - Firebox 15"
104 3 285.0000 Remote Order Printer - Epson
97 4 395.0000 Aldelo Lite
121 5 549.0000 Cash Register Express - Pro
191 6 349.0000 Integrated Payment Processing
155 7 369.0000 Accessory - Posiflex 12.1" LCD Customer Display
答案 0 :(得分:1)
这不是GROUP BY应该如何运作的。如果按多个列分组,则您的选择只能返回:
所以你需要这样做:
SELECT
bs.step_number,
MIN(p.price) AS min_price, pd.name as product_name
FROM
builder_product bp
JOIN builder_step bs ON bp.builder_step_id = bs.builder_step_id
JOIN builder b ON bp.builder_id = b.builder_id
JOIN product p ON p.product_id = bp.product_id
JOIN product_description pd ON p.product_id = pd.product_id
WHERE b.builder_id = '74' and bs.optional != '1'
group by bs.step_number, pd.name
ORDER by bs.step_number, min_price
(MySQL允许非常放松的语法,并且会很乐意删除每个组的随机行,但其他DBMS会触发原始查询的错误。)
答案 1 :(得分:1)
加入到只包含每组最小值的表的子选择
在这个例子中。 mygroup min(amt)返回组的最低美元项
然后我将它作为完整的内部联接返回到主表,将记录限制为最小值。
Select A.myGROUP, A.Amt
from mtest A
INNER JOIN (Select myGroup, min(Amt) as minAmt from mtest group by mygroup) B
ON B.myGroup=A.mygroup
and B.MinAmt = A.Amt
答案 2 :(得分:0)
是。每个不同的组键仅返回一次。这个问题不容易解决。运行两个不同的查询并在之后合并结果。如果这不是一个选项,则为每个步骤创建一个临时表,以便在查询中加入表格。