2

我是第一次发帖。

我一直试图找出这份报告的问题。在此报告中,我想显示最近已售出但未退回(+ 非 - 价格)的产品的序列号。

我想检查一个产品是否被标记为已售出,如果它最后售出并且没有最后退回。

这就是我所做的,我想我可能需要对其进行子查询,但我无法弄清楚第一部分。

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`

请帮忙。提前致谢。乔

4

1 回答 1

1

好的,感谢我自己。但是对于其他有这种问题的人,你去吧。

三重巢穴,哈哈

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
于 2013-05-21T21:54:33.557 回答