Mysql:如何检索基于 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
的数据,其结果如下:
- Qty = 20
- Qty = 8
- Qty = 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 |
I have confused how to retrieve some data from MySQL database. I have the query like:
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
I want to show data from COUNT(A.Serial_number) AS Qty
which have result like:
- Qty = 20
- Qty = 8
- Qty = 32
How do I do to retrieve that?
result like:
|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 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行
GROUP BY
后,您可以使用HAVING
来设置要保留聚合数据的条件:最好不要将
LIKE
与数字一起使用字段或计算结果。 此条件:当
Qty
是整数时,可以写为:或等效地写为:
After you do a
GROUP BY
, you can useHAVING
to put conditions on which aggregate data to keep:It's best not to use
LIKE
with numerical fields or computed results. This condition:can be written when
Qty
is an integer as:or equivalently as:
到目前为止,我已经尝试自己使用
SELECT inside SELECT
:之后我得到了我想要的。
或者
使用HAVING
(最简单的方法)。So far, i have tried by own self use
SELECT inside SELECT
:After that i get what I want.
OR
USE HAVING
(the simplest way).