0

我想从表 PRODUCT_INFORMATION 中选择所有内容。我尝试使用 p.* 和 PRODUCT_INFORMATION.* 但它不起作用?我的错误在哪里?与JOIN有关吗?

SELECT  
p.PRODUCT_NAME , 
SUM (oi.QUANTITY) TOTAL_QUANTITY, 
o.ORDER_MODE

FROM PRODUCT_INFORMATION p
    LEFT JOIN ORDER_ITEMS oi ON p.PRODUCT_ID = oi.PRODUCT_ID
    LEFT JOIN ORDERS o ON o.ORDER_ID = oi.ORDER_ID
        WHERE INSTR (p.PRODUCT_NAME, '<') = 0
        AND INSTR (p.PRODUCT_NAME, '_') = 0
    GROUP BY p.PRODUCT_NAME, p.WARRANTY_PERIOD, o.ORDER_MODE
    HAVING SUM (oi.QUANTITY) > 200
    ORDER BY p.WARRANTY_PERIOD;
4

1 回答 1

0

在 MySQL 中,您可以通过表的主键聚合并仍然接受所有列。假设这PRODUCT_ID是主键,您可以使用:

SELECT p.*, SUM(oi.QUANTITY) as TOTAL_QUANTITY, o.ORDER_MODE
FROM PRODUCT_INFORMATION p LEFT JOIN
     ORDER_ITEMS oi
     ON p.PRODUCT_ID = oi.PRODUCT_ID LEFT JOIN
     ORDERS o
     ON o.ORDER_ID = oi.ORDER_ID
WHERE INSTR (p.PRODUCT_NAME, '<') = 0 AND
      INSTR (p.PRODUCT_NAME, '_') = 0
GROUP BY p.PRODUCT_ID, o.ORDER_MODE
HAVING SUM (oi.QUANTITY) > 200
ORDER BY p.WARRANTY_PERIOD;
于 2021-09-17T02:28:34.023 回答