English 中文(简体)
我怎么会问,如何照顾到两个不同的数据组,并得出取决于MySQL第一组结果?
原标题:How do I create a query that caters for two different groups of data and produces a result that is dependent on the first set in MySQL?

我试图计算所有数据组有一个或多个故障的情况。

我也认为,很难解决这一问题,因此,我ping希望,在解释我想要达到的目标时,举一个例子。

抽样数据:

INSERT INTO test.answers (id, result_id, fail_all, fail_group) VALUES
    (1,1,0,1),  (2,1,0,1),  (3,1,0,1),  (4,1,0,0),
    (5,2,1,0),  (6,2,0,0),  (7,2,1,0),  (8,2,1,0),  (9,2,1,0),
    (10,3,0,1), (11,3,1,1), (12,3,0,1), (13,3,0,1), (14,3,0,1),
    (15,4,0,0), (16,4,0,0), (17,4,0,1), (18,4,0,1), (19,4,0,0), (20,4,0,1),
    (21,5,1,0), (22,5,0,1), (23,5,1,1), (24,5,0,1), (25,5,1,0), (26,5,0,1);

INSERT INTO test.results (id,team_id) VALUES
    (1,1), (2,1), (3,1), (4,2), (5,2);

接着,我提出以下问题:

SELECT
    COUNT(IF(a.fail_all = 1,1,NULL)) AS count_fail_all,
    COUNT(IF(a.fail_group = 1,1,NULL)) AS count_fail_group,
    a.result_id
FROM test.answers AS a
GROUP BY a.result_id

结果:

count_fail_all, count_fail_group, result_id
0, 3, 1
4, 0, 2
1, 5, 3
0, 3, 4
3, 4, 5

我需要提出一个问题,即由团队组成的团体,并估计有多少失败。 如果结果不止一次失败,那么总体结果就失败了。 因此,如果上述查询中的成果数为3(例:初步结果),则只能算作1。 失败的集团现在可以被忽略,因为我认为失败的同样解决办法——都会为失败而努力。

我希望其结果是:

team_id, amount_of_fails, amount_of_fails_per_group
1, 2, 2
2, 1, 2

I hope someone might be able to help me create the query that I need? I m not even sure how to start. Let me know if there is anything that I can do to adjust the query as I know it s not very well asked?

Thanks!

问题回答

如果你想要这一产出的话。 或许可以这样说:

SELECT
  r.team_id,
  SUM(IF(a.fail_all = 1,1,0)) AS amount_fail_all,
  SUM(IF(a.fail_group = 1,1,0)) AS amount_fail_group
 FROM answers AS a
 JOIN results AS r on r.id=a.result_id
 GROUP BY r.team_id

这将使你获得这一产出:

team_id  amount_fail_all  amount_fail_group
1        2                2
2        1                2




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签