0

我正在研究一个 mysql 查询,它将根据返回的行数过滤掉某些事件。

当返回的行数为 1 时,我试图过滤掉任何支持类别,但是当返回的结果集大于 1 时,保留支持类别。

我最初有这个想法,但它似乎行不通。

SELECT stockmaster.description, SUM(salesorderdetails.quantity), stockmaster.categoryid         as qty 
FROM salesorderdetails, stockmaster 
where salesorderdetails.stkcode=stockmaster.stockid 
and orderno='5222'
group by stockmaster.description
HAVING CASE WHEN stockmaster.categoryid = 'S&M' THEN COUNT(*) >= 2 ELSE COUNT(*) = 1   END

任何帮助将不胜感激。

4

1 回答 1

0

尝试这个

SELECT *
FROM
(

SELECT stockmaster.description, 
       SUM(salesorderdetails.quantity), 
       stockmaster.categoryid         as qty ,
       COUNT(*) AS count
FROM salesorderdetails, stockmaster 
where salesorderdetails.stkcode=stockmaster.stockid 
and orderno='5222'
group by stockmaster.description
HAVING CASE WHEN stockmaster.categoryid = 'S&M' 

) MAIN_DATA

WHERE MAIN_DATA.count >1
于 2013-07-19T12:21:02.250 回答