我想获得产品的名称,但是保持相同的销售额,加入时我只获得1条记录,但是我想要获得3条销售记录

产品展示

mysql - 如何从另一个表中获取数据而无需加入mysql-LMLPHP
营业额

mysql - 如何从另一个表中获取数据而无需加入mysql-LMLPHP

我的SQL查询

SELECT p.name as 'Descripción', p.cost as 'Precio de costo',p.price as 'Precio de venta',
sum((p.price-ps.discount)+(ps.discount/ps.qty)) as 'precio con descuento', ps.discount as 'descuento', ps.qty as 'cantidad',
ps.total as 'Sub total venta', (sum((p.price-ps.discount)+(ps.discount/ps.qty))-p.cost)/ps.qty as 'Utilidad',
sum((p.price-ps.discount)+(ps.discount/ps.qty))-p.cost as 'Sub Total Utilidad'
FROM product_sales ps
inner join products p on p.id = ps.product_id


这是我希望获得的结果,
3条记录,而不是1条。

mysql - 如何从另一个表中获取数据而无需加入mysql-LMLPHP

最佳答案

您不需要聚合函数SUM(),因为您无需对结果的行进行聚合。
另外,您必须正确计算列Sub total venta

SELECT
  p.name as `Descripción`,
  p.cost as `Precio de costo`,
  p.price as `Precio de venta`,
  (p.price-ps.discount)+(ps.discount/ps.qty) as `precio con descuento`,
  ps.discount as `descuento`,
  ps.qty as `cantidad`,
  p.price * ps.qty - ps.discount as `Sub total venta`,
  (((p.price-ps.discount)+(ps.discount/ps.qty))-p.cost)/ps.qty as `Utilidad`,
  (p.price-ps.discount)+(ps.discount/ps.qty)-p.cost as `Sub Total Utilidad`
FROM product_sales ps INNER JOIN products p
ON p.id = ps.product_id

关于mysql - 如何从另一个表中获取数据而无需加入mysql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59695119/

10-11 02:07