English 中文(简体)
I.ql query, grouping, if,s
原标题:mysql query, grouping, if, etc
  • 时间:2011-01-26 21:02:13
  •  标签:
  • mysql

我要问:

SELECT
  COUNT(*),
  SUBSTR(datetime_acc, 1, 10),
  type_acc
FROM payment_acc
WHERE type_acc LIKE  %monthly% 
GROUP BY SUBSTR(datetime_acc, 1, 10), type_acc
ORDER BY datetime_acc, type_acc
 count  date        type_acc       
------  ----------  ---------------
     4  2011-01-20  monthly payment
     3  2011-01-20  failed monthly 
     5  2011-01-21  monthly payment
    10  2011-01-22  failed monthly 
    11  2011-01-22  monthly payment
     1  2011-01-23  monthly payment
     4  2011-01-24  monthly payment
     1  2011-01-24  failed monthly 
     6  2011-01-25  failed monthly 
     6  2011-01-25  monthly payment
    16  2011-01-26  failed monthly 
    10  2011-01-26  monthly payment

但是,我试图让失败,并在不同的栏目中一行走。

success  failed  date        
-------  ------  ----------  
      4       3  2011-01-20  
      5      10  2011-01-21  
     11    NULL  2011-01-22  
      1    NULL  2011-01-23  
      4       1  2011-01-24  
      6       6  2011-01-25  
     10      16  2011-01-26  

Any clues?

最佳回答

2. 提出以下意见:

SELECT
    T1.date,
    T2.count AS success_count,
    T3.count AS failed_count
FROM yourview T1
LEFT JOIN yourview T2 ON T1.date = T2.date AND T2.type_acc =  monthly payment 
LEFT JOIN yourview T3 ON T1.date = T3.date AND T3.type_acc =  failed monthly 
GROUP BY T1.date

成果:

date        success_count  failed_count
2011-01-20  4              3           
2011-01-21  5              NULL        
2011-01-22  11             10          
2011-01-23  1              NULL        
2011-01-24  4              1           
2011-01-25  6              6           
2011-01-26  10             16          
问题回答

排位是我唯一看到这种可能性的方法。 这的确意味着许多表格。 利用EXPLAIN方案,看看看它如何。

SELECT b.intSuccess, c.intFailed, a.dateThis FROM (SELECT DATE(datetime_acc) AS dateThis FROM payment_acc) a LEFT JOIN (SELECT COUNT(*) AS intFailed, DATE(datetime_acc) AS dateThis FROM payment_acc WHERE type_acc LIKE "%failed%" AND type_acc LIKE "%monthly%" GROUP BY DATE(datetime_acc)) b ON b.dateThis=a.dateThis LEFT JOIN (SELECT COUNT(*) AS intSuccess, DATE(datetime_acc) AS dateThis FROM payment_acc
WHERE type_acc NOT LIKE "%failed%" AND type_acc LIKE "%monthly%" GROUP BY (datetime_acc)) c ON c.dateThis=a.dateThis ORDER BY a.dateThis;





相关问题
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 ...

热门标签