我是第一次发帖。
我一直试图弄清楚这份报告的问题。 在本报告中,我想显示最近销售但未退货(+非 - 价格)的产品的序列号。
我想检查一个产品是否已被标记为已销售,如果它是最后一次销售而且最后没有返回。
这就是我的方式,我想我可能需要对它进行子查询,但我无法弄清楚第一部分。
SELECT
ps.serial,p.upc,p.name,so.date_sold
FROM
store_orders so
LEFT JOIN store_order_products sop ON so.id = sop.order_id
LEFT JOIN products p ON sop.product_id = p.id
LEFT JOIN product_serials ps ON ps.serial = sop.product_serial
WHERE
(CAST(so.date_sold AS DATE) BETWEEN '2013-4-15' AND '2013-5-15')
AND ps.deleted = '0' AND sop.price > 0
ORDER BY
sop.id DESC
LIMIT 10
它返回的是最后退回的产品,而不是最后销售的产品。
`ID Serial Price
1: 8332-83 10.00
2: 8332-83 -10.00
Dont want to see this because it was returned last, not sold last
ID Serial Price
2: 8332-82 10.00
1: 8332-82 -10.00
This was sold last not returned so looking to see this serial`
请帮忙。 提前致谢。 乔
答案 0 :(得分:1)
好的,感谢我自己。但对于有这类问题的其他人,请你走了。
TRIPLE NESTY,哈哈
SELECT * FROM (
SELECT * FROM (
SELECT
so.id,sop.product_serial,p.upc,sop.price,p.name,so.date_sold
FROM
store_orders so
LEFT JOIN store_order_products sop ON so.id = sop.order_id
LEFT JOIN products p ON sop.product_id = p.id
LEFT JOIN product_serials ps ON ps.serial = sop.product_serial
WHERE ps.deleted = '0' AND so.store_id = '$store_id'
ORDER BY
sop.id DESC
LIMIT 1000
) AS t
GROUP BY t.product_serial
) AS t2
WHERE t2.price > 0