如何将这2个sql查询编码为1以获得我需要的结果

时间:2016-06-11 15:19:36

标签: mysql sql join merge union

我有一个复杂的查询,几乎可以产生我需要的一切 见:

SELECT suppliers_prods.reference,products.id,products.price,products.current,suppliers_prods.price,
suppliers_prods.costoverhead,stock.total,stock.location,products.maxstock,manufacturers.name,products.title,products.reference,products.weight,suppliers.name,products.manufacturerref,stock.location,suppliers_prods.adjusted_overhead,
suppliers_prods.1_of_trade_perc,suppliers_prods.more_than,suppliers_prods.multiple_of_trade_perc,
suppliers_prods.last_order_amount,DATE_FORMAT(suppliers_prods.last_ordered,"%d-%m-%Y %T") AS supplier_last_ordered,prodtypes.description AS prodtype, DATE_FORMAT(MAX(orders.orderdate),"%d-%m-%Y %T") AS item_last_ordered, SUM(wishlist.qty) AS notify_waiting_qty

FROM products

INNER JOIN prodtypes ON products.prodtype=prodtypes.id

INNER JOIN stock ON products.id=stock.prodid

INNER JOIN suppliers_prods ON suppliers_prods.prodid=products.id

INNER JOIN suppliers ON suppliers_prods.supplierid=suppliers.id

INNER JOIN manufacturers ON products.manufacturer=manufacturers.id

LEFT JOIN wishlist on products.id=wishlist.prodid

LEFT JOIN order_items ON products.id=order_items.itemid

LEFT JOIN orders ON order_items.orderid=orders.id

WHERE products.current = "1" AND suppliers.name = "ICS"

GROUP BY products.id

ORDER BY products.current,stock.total;

问题是字段notify_waiting_qty中包含错误的数据。

我可以运行以下查询:

select suppliers_prods.reference,wishlist.prodid,count(DISTINCT wishlist.custid) as qty
from wishlist
INNER JOIN products ON products.id=wishlist.prodid
INNER JOIN suppliers_prods ON suppliers_prods.prodid=products.id
where 
DATE_ADD(wishlist.date_added,INTERVAL 2 YEAR) >= CURDATE() and 
wishlist.notified = "0"
group by wishlist.prodid

这为我提供了字段QTY,其中包含字段suppliers_prods.reference的正确信息。

我可以将数据组合在一起的唯一方法是在Excel中:我将查询1的结果粘贴到Excel的第1页,然后将2查询到第2页。

然后在工作表notify_waiting_qty上使用字段suppliers_prods.reference我查找了sheet2并查找了QTY的值并将其放入工作表

因此SQL查询1的结果是(它是csv文件的副本):

suppliers_prods.reference,products.id,products.price,products.current,suppliers_prods.price,suppliers_prods.costoverhead,stock.total,stock.location,products.maxstock,manufacturers.name,products.title,products.reference,products.weight,suppliers.name,products.manufacturerref,stock.location,suppliers_prods.adjusted_overhead,suppliers_prods.1_of_trade_perc,suppliers_prods.more_than,suppliers_prods.multiple_of_trade_perc,suppliers_prods.last_order_amount,.supplier_last_ordered,prodtypes.prodtype,.item_last_ordered,.notify_waiting_qty
MA-09,2686,15,1,3.11,1.17,0,M1-R3-S4-P2-D1,15,ICS,ICS M4 Hop Up Unit,ICS-MA-09,99,ICS,MA-09,M1-R3-S4-P2-D1,1.17,5.56,1,5.56,15,31/03/2016 15:28,Hopup,15/05/2016 18:40,16378

查询2的结果:

suppliers_prods.reference,wishlist.prodid,.qty
MA-09,2686,2

因此,在我的Excel电子表格中并使用Excel命令,我可以将MA-09 QTY值2输入到MA-09行字段notify_waiting_qty中,从而产生一行包含我需要查看的所有数据:< / p>

suppliers_prods.reference,products.id,products.price,products.current,suppliers_prods.price,suppliers_prods.costoverhead,stock.total,stock.location,products.maxstock,manufacturers.name,products.title,products.reference,products.weight,suppliers.name,products.manufacturerref,stock.location,suppliers_prods.adjusted_overhead,suppliers_prods.1_of_trade_perc,suppliers_prods.more_than,suppliers_prods.multiple_of_trade_perc,suppliers_prods.last_order_amount,.supplier_last_ordered,prodtypes.prodtype,.item_last_ordered,.notify_waiting_qty
MA-09,2686,15,1,3.11,1.17,0,M1-R3-S4-P2-D1,15,ICS,ICS M4 Hop Up Unit,ICS-MA-09,99,ICS,MA-09,M1-R3-S4-P2-D1,1.17,5.56,1,5.56,15,31/03/2016 15:28,Hopup,15/05/2016 18:40,2

我用来从表2到表1获取数据的Excel语句是:

=IF(ISNA(VLOOKUP(A2, Sheet2!$A$1:$O$2999,3, FALSE)),0,VLOOKUP(A2, Sheet2!$A$1:$O$2999,3, FALSE))

我要求的可能吗? 如果是,我该如何编码 我认为Union会为同一个suppliers_prods.reference提供2行数据,但我只想为每个suppliers_prods.reference提供一行数据。

查询1上复杂代码的原因是我需要最新的订单日期值,这段代码让我知道了。

0 个答案:

没有答案