Mysql:如何检索基于COUNT()结果的数据

时间:2011-05-11 03:53:41

标签: mysql select count

我很困惑如何从MySQL数据库中检索一些数据。我有这样的查询:

inspection_report table:
 - Inspection_datetime
 - Line
 - Model
 - Lot_no
 - Serial_number
 - Shift
 - Range_sampling
 - Accesories
 - Acc_qty
Employee table:
 - NIK
 - name

SELECT DATE(A.Inspection_datetime) AS Date, A.Line,TRIM(A.Model) AS Model, A.Lot_no,
       COUNT(A.Serial_number) AS Qty,GROUP_CONCAT(DISTINCT(A.Shift)) AS Shift,
       IF(RIGHT(A.Range_sampling,4)='0000',10000,RIGHT(A.Range_sampling,4))-MID(A.Range_sampling,5,4)+1 AS Merchandise,
       A.Accesories,A.Acc_qty,A.Range_sampling,B.name
FROM inspection_report A
LEFT JOIN Employee B
ON A.NIK=B.NIK
WHERE CHAR_LENGTH(A.Range_sampling) = 17 AND A.Issue='' AND A.Check='' AND A.Approve=''  
GROUP BY A.Line, A.Model, A.Lot_no, A.Range_sampling
ORDER BY Date DESC,A.Line

我想显示来自COUNT(A.Serial_number) AS Qty的数据,结果如下:

  • 数量= 20
  • 数量= 8
  • 数量= 32

如何检索?


结果如:

|Date      |Line   |Model |Lot_no| Qty|Shift|Merchandise|Accesories|Acc_qty|Range_sampling|name |
+----------+-------+------+------+----+-----+-----------+----------+-------+--------------+-----+
|2011-05-12| fa 02 |BlaBla|021A  | 20 |  A  |  200      |  OK      |11     |   1-200      |tom  |
|2011-05-12| fa 15 | foo  |021A  | 8  |  A  |  200      |  OK      |11     |   1-200      |Bill |
|2011-05-12| fa 01 | Boom |021A  | 32 |  A  |  200      |  OK      |11     |   1-200      | Jim | 

2 个答案:

答案 0 :(得分:1)

到目前为止,我已尝试过自己使用SELECT inside SELECT

SELECT X.Date,X.Line,X.Model,X.Qty,...........
FROM(
      SELECT DATE(A.Inspection_datetime) AS Date, A.Line,TRIM(A.Model) AS Model, A.Lot_no,
             COUNT(A.Serial_number) AS Qty,GROUP_CONCAT(DISTINCT(A.Shift)) AS Shift,
             IF(RIGHT(A.Range_sampling,4)='0000',10000,RIGHT(A.Range_sampling,4))-MID(A.Range_sampling,5,4)+1 AS Merchandise,
             A.Accesories,A.Acc_qty,A.Range_sampling,B.name
      FROM inspection_report A
      LEFT JOIN Employee B
      ON A.NIK=B.NIK
      WHERE CHAR_LENGTH(A.Range_sampling) = 17 
      AND A.Issue='' AND A.Check='' AND A.Approve=''  
      GROUP BY A.Line, A.Model, A.Lot_no, A.Range_sampling
      ORDER BY Date DESC,A.Line) X
WHERE X.Qty LIKE '20' OR X.Qty LIKE '8' OR X.Qty LIKE '32'

之后我得到了我想要的东西。 或USE HAVING(最简单的方法)。

答案 1 :(得分:1)

执行GROUP BY后,您可以使用HAVING来设置要保留汇总数据的条件:

SELECT DATE(A.Inspection_datetime) AS Date
     , A.Line
     , TRIM(A.Model) AS Model
     , A.Lot_no
     , COUNT(A.Serial_number) AS Qty
     , GROUP_CONCAT(DISTINCT(A.Shift)) AS Shift
     ,   IF( RIGHT(A.Range_sampling,4)='0000'
           , 10000
           , RIGHT(A.Range_sampling,4)
           )
       - MID( A.Range_sampling, 5, 4)
       + 1
       AS Merchandise
     , A.Accesories
     , A.Acc_qty
     , A.Range_sampling
     , B.name
FROM inspection_report A
    LEFT JOIN Employee B
       ON A.NIK=B.NIK
WHERE CHAR_LENGTH(A.Range_sampling) = 17 
  AND A.Issue='' 
  AND A.Check='' 
  AND A.Approve=''  
GROUP BY A.Line
       , A.Model
       , A.Lot_no
       , A.Range_sampling
HAVING COUNT(A.Serial_number) IN (20, 8, 32)
ORDER BY Date DESC
       , A.Line

最好不要将LIKE与数字字段或计算结果一起使用。这个条件:

Qty LIKE '20' OR Qty LIKE '8' OR Qty LIKE '32'
Qty是一个整数时,可以写

Qty = 20 OR Qty = 8 OR Qty = 32

或等同于:

Qty IN (20, 8, 32)