我如何选择数据和子查询分组依据

时间:2018-10-02 07:45:39

标签: php codeigniter

当我选择数据时,仅显示前lot个数据。但我需要所有lot数据。 这是我的查询:

SELECT lot,
(select count(pass) FROM pass_fail_result where pass=0) toatl_fail,
(select count(pass) FROM pass_fail_result where pass=1) toatl_pass FROM pass_fail_result group by lot;

我想显示所有通过结果,例如pass=10fail=2 enter image description here

1 个答案:

答案 0 :(得分:0)

最简单的方法是通过条件聚合,我们计算或求和以失败或通过记录为目标的CASE表达式:

SELECT
    lot,
    COUNT(CASE WHEN pass = 0 THEN 1 END) AS toatl_fail,
    COUNT(CASE WHEN pass = 1 THEN 1 END) AS toatl_pass
FROM pass_fail_result
GROUP BY
    lot;