比较两年的日期

时间:2016-04-15 14:03:54

标签: mysql sql date

我有一张人口表,我希望在两年内比较人口。

我的桌子结构:
id(自动增量),类型(男人,女人,孩子),人口(1到10000),日期

我想在查询下运行两个并显示到一个表结果中:

query1:

SELECT type,count(population) as count_of_year1
FROM population
where date between '2013-01-01' and '2013-01-24'
GROUP BY type

QUERY2:

SELECT type, count(population) as count_of_year2
FROM population
where date between '2014-01-01' and '2014-01-24'
GROUP BY type

我需要这个结果:

|输入| 2013年人口| 2014年的人口

怎么做?

2 个答案:

答案 0 :(得分:0)

使用SELECT type, count(case when date between '2013-01-01' and '2013-01-24' then population end) as count_of_year1, count(case when date between '2014-01-01' and '2014-01-24' then population end) as count_of_year2 FROM population GROUP BY type 表达式进行条件计数:

where

添加此where date between '2013-01-01' and '2013-01-24' or date between '2014-01-01' and '2014-01-24' 子句以加快速度:

    class ViewController: UIViewController, AFDXDetectorDelegate {

    var detector : AFDXDetector? = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        // create the detector
        detector = AFDXDetector(delegate:self, usingCamera:AFDX_CAMERA_FRONT, maximumFaces:1)
        detector?.setDetectEmojis(true)
        detector!.start()
    }

    func detectorDidStartDetectingFace(face : AFDXFace) {
        // handle new face
    }

    func detectorDidStopDetectingFace(face : AFDXFace) {
        // handle loss of existing face
    }

    func detector(detector : AFDXDetector, hasResults : NSMutableDictionary?, forImage : UIImage, atTime : NSTimeInterval) {
        // handle processed and unprocessed images here
        if hasResults != nil {
            // handle processed image in this block of code

            // enumrate the dictionary of faces
            for (_, face) in hasResults! {
                // for each face, get the rage score and print it
                let emoji : AFDXEmoji = face.emojis
                let rageScore = emoji.rage
                print(rageScore)
            }                
        } else {
            // handle unprocessed image in this block of code
        }
    }

答案 1 :(得分:0)

由于种群的值可以是1到10000,我假设你想要SUM(),而不是COUNT()。

我有一个单独的表类型:

  • population_type - id,title
  • population - id,type_id(引用type.id),人口,日期

然后我更喜欢在这里使用JOIN:

    SELECT pt.title type,
           COALESCE(y1.total_population,0) population_2013,
           COALESCE(y2.total_population,0) population_2014
      FROM population_type pt
 LEFT JOIN (
        SELECT type_id,
               SUM(population) total_population,
          FROM population
         WHERE date >= '2013-01-01'
           AND date < '2013-01-24' + INTERVAL 1 DAY           
      GROUP BY type
           ) y1
        ON y1.type_id = pt.id          
 LEFT JOIN (
        SELECT type_id,
               SUM(population) total_population,
          FROM population
         WHERE date >= '2014-01-01'
           AND date < '2014-01-24' + INTERVAL 1 DAY           
      GROUP BY type
           ) y2
        ON y2.type_id = pt.id  

这样,您每次只需要总结您需要的内容,并且查询更加模块化。

相关问题