mysql:需要简短查询

时间:2013-06-16 07:28:45

标签: mysql if-statement sum group-concat

我有查询,它给出了正确的结果,但是,它有内部查询,重复相同的条件。我试图尽可能缩短查询的长度。

任何人都可以帮助我降低查询的复杂性,因为在服务器上花费12分钟才能得到结果,这可能会在将来导致很多问题。

问题在于,重复相同的条件,并且查询在其中使用内部查询。

使用这些mysql参数进行查询: -

求和,如果,GROUP_CONCAT

查询: -

select 
sum( 
IF(priority="P1",1,0)) P1,
sum( 
IF((timediff(delta_ts,creation_ts) > "00:02:00") 
 && (priority="P1") ,1,0))P1_exeeded,
 (select 
 GROUP_CONCAT( DISTINCT bug_id) 
 from bugs 
 where  
 ( 
   IF((timediff(delta_ts,creation_ts) > "00:02:00") && (priority="P1") 
   && (product_id=237)
   &&(bugs.resolution='FIXED')
   &&(bug_status="RESOLVED")
   &&(bugs.creation_ts >='2013-06-14 09:00:00' 
   && bugs.creation_ts <= '2013-06-16 08:59:59') ,1,0)
 )
) as bug_ids,

SUM(
IF(priority="P2",1,0)) P2,
sum( 
IF((timediff(delta_ts,creation_ts) > "00:01:00") 
&& (priority="P2") ,1,0))P2_exeeded,
(select GROUP_CONCAT( DISTINCT bug_id) 
 from bugs 
 where
 ( 
 IF((timediff(delta_ts,creation_ts) > "00:02:00") 
 && (priority="P2") 
 && (product_id=237)&&(bugs.resolution='FIXED')
 &&(bug_status="RESOLVED")&&(bugs.creation_ts >='2013-06-14 09:00:00' 
 && bugs.creation_ts <= '2013-06-16 08:59:59') ,1,0)
 )
 ) as bug_ids,
 SUM(
 IF(priority="P3",1,0)) P3count,
 SUM(
 IF(priority="P4",1,0)) P4count 
 from bugs 
 where bugs.product_id=237 
 and bugs.resolution='FIXED' 
 and bugs.creation_ts >='2013-06-14 09:00:00' 
 and bugs.creation_ts <= '2013-06-16 08:59:59' 
 and bug_status="RESOLVED";

结果: -

+------+------------+---------+------+------------+---------+---------+---------+
| P1   | P1_exeeded | bug_ids | P2   | P2_exeeded | bug_ids | P3count | P4count |
+------+------------+---------+------+------------+---------+---------+---------+
|    7 |          1 | 3743304 |    6 |          1 | 3743305 |       5 |       1 |
+------+------------+---------+------+------------+---------+---------+---------+
  • 为了获取bug id,我使用了group concat。查询的复杂性

1 个答案:

答案 0 :(得分:0)

得到了答案: -

解决它: -

select 
sum(
IF(
priority="P1",1,0)) P1, 
sum(
IF(
(timediff(delta_ts,creation_ts) > "00:02:00") && (priority="P1") ,1,0)) P1_exeeded,
REPLACE(GROUP_CONCAT(
IF(
(timediff(delta_ts,creation_ts) > "00:02:00") 
  && (priority="P1") ,bug_id,'')),',,','' ) as bugids,  
SUM(
IF(priority="P2",1,0)) P2, 
sum( IF((timediff(delta_ts,creation_ts) > "00:01:00") 
 && (priority="P2") ,1,0))P2_exeeded,
REPLACE(GROUP_CONCAT(IF((timediff(delta_ts,creation_ts) > "00:02:00") 
 && (priority="P2") ,bug_id,'')),',,','' ) as bugids ,
SUM(
IF(priority="P3",1,0)) P3count, 
sum( 
IF(
(timediff(delta_ts,creation_ts) > "00:02:00") 
&& (priority="P3"),1,0))P3_exeeded,
REPLACE(GROUP_CONCAT(
IF((timediff(delta_ts,creation_ts) > "00:02:00") 
 && (priority="P3") ,bug_id,'')),',,','' ) asbugids, 
SUM(
 IF(priority="P4",1,0)) P4count,
sum(
 IF(
(timediff(delta_ts,creation_ts) > "00:02:00") 
 && (priority="P4") ,1,0)) P4_exeeded,
 REPLACE(
 GROUP_CONCAT(
  IF(
  (timediff(delta_ts,creation_ts) > "00:02:00") 
  && (priority="P4") ,bug_id,'')),',,','' ) as bugids 
  from bugs 
  where bugs.product_id=237 
  and bugs.resolution  ='FIXED' 
  and bugs.creation_ts >='2013-06-14 09:00:00' 
  and bugs.creation_ts <= '2013-06-16 08:59:59' 
  and bug_status="RESOLVED";

我在 0.01秒

中得到了结果
+------+------------+-----------+------+------------+---------+---------+------------+--------+---------+------------+--------+
| P1   | P1_exeeded | bugids    | P2   | P2_exeeded | bugids  | P3count | P3_exeeded | bugids | P4count | P4_exeeded | bugids |
+------+------------+-----------+------+------------+---------+---------+------------+--------+---------+------------+--------+
|    7 |          1 | ,3743304, |    6 |          1 | 3743305 |       5 |          0 |        |       1 |          0 |        |
+------+------------+-----------+------+------------+---------+---------+------------+--------+---------+------------+--------+

谢谢大家,我使用了replace,而不是内部查询,并使用了group con cat,没有内部查询。