mysql:尝试选择未售出的商品

时间:2016-06-25 19:44:47

标签: mysql

一个新手问题,可能与Mysql一样多的逻辑......

我有一个针对各种产品的报价表,如果报价导致订单,则会有订单日期:

quoteID     product    sellPrice       orderDate
  1           apple       1.00          5-29-1983  
  2           pear        1.35            null
  3           pear        1.25          6-18-1983
  4           banana      1.25            null

我正在尝试从未销售过的产品。我知道我可以计算每个项目被引用的次数(不同的产品),但这给了我apple 1; pear 2; banana 1。我想要的是Banana 0,或者可能更有用apple 1,1; pear 2,1; banana 1,0来显示报价单比率。

对此的任何帮助将不胜感激。提前谢谢!

2 个答案:

答案 0 :(得分:0)

您应该使用subselect而不是

 select product from my_table 
 where product not in (select product from my_table where orderDate is not null)

答案 1 :(得分:0)

感谢您回答scaisEdge和评论Drew。正如Drew预测的那样,"NOT IN"的建议答案不起作用。我最终加入"null order table" "order table"并且似​​乎运作良好(见下文)。欢迎对这种方法或细节进行评论!

<强>结果: “产品”, “订购日期”, “产品”, “订购日期”, “countOrders” “香蕉”,NULL,NULL,NULL,NULL

<强>查询:

SELECT * FROM 
(select product, orderdate  from quotes 
where orderdate IS NULL
group by product) as t1
LEFT JOIN
(select product, orderdate, count(product) 'countOrders' from quotes 
where orderdate is NOT NULL
group by product) as t2
on t1.product=t2.product
where countOrders is NULL