MYSQL根据返回的行检索数据

时间:2013-07-19 12:11:45

标签: mysql case-expression

我正在开发一个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

任何帮助都将被感激地接受。

1 个答案:

答案 0 :(得分: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