如何计算SQLite中某个表中外来表id的出现次数?

时间:2015-10-12 06:33:43

标签: android sqlite ormlite

所以我有下表:

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var collectionView1: UICollectionView!
    @IBOutlet weak var collectionView2: UICollectionView!

    let collectionViewAIdentifier = "CollectionViewACell"
    let collectionViewBIdentifier = "CollectionViewBCell"

    var imageArray = [UIImage]()

    override func viewDidLoad() {
        super.viewDidLoad()

        imageArray = [UIImage(named: "1.jpg")!, UIImage(named: "2.jpg")!, UIImage(named: "3.jpg")!, UIImage(named: "4.jpg")!, UIImage(named: "5.jpg")!, UIImage(named: "6.jpg")!, UIImage(named: "7.jpg")!, UIImage(named: "8.jpg")!, UIImage(named: "9.jpg")!, UIImage(named: "10.jpg")!, UIImage(named: "11.jpg")!, UIImage(named: "12.jpg")!, UIImage(named: "13.jpg")!]
        print(imageArray)

        collectionView1.delegate = self
        collectionView2.delegate = self

        collectionView1.dataSource = self
        collectionView2.dataSource = self

    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        if collectionView == self.collectionView1 {

            let cellA = collectionView1.dequeueReusableCellWithReuseIdentifier(collectionViewAIdentifier, forIndexPath: indexPath) as! CollectionViewCell1
            cellA.imageV.image = imageArray[indexPath.row]

            return cellA
        }

        else {
            let cellB = collectionView2.dequeueReusableCellWithReuseIdentifier(collectionViewBIdentifier, forIndexPath: indexPath) as! CollectionViewCell2

            cellB.imageV.image = imageArray[indexPath.row]
            return cellB
        }
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return imageArray.count
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        if collectionView == self.collectionView2{
            collectionView1.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
        }
    }

}

我想计算表B中Table A a_id b_id Table B b_id b_name 显示或用作表A中id列中的值的次数。可能,结果应该是这样的:

b_id

我考虑使用以下查询:

ID     count     name
1      10        aaaa
2      8         bbbb
3      11        cccc

获取所有ID。然后迭代它们中的每一个并像这样计算它们:

Select b_id from table_b 

但这个过程太长了。我想在一个查询中缩短一点。但我不知道如何使用SQLite语言迭代每一行。

(即使只是推向正确的方向也会有所帮助。)

2 个答案:

答案 0 :(得分:2)

以下sql查询应该有效:

   SELECT      a.a_id, 
               COUNT(b.b_id)    
   FROM        Table_A a
   JOIN  Table_B b on a.b_id=b.b_id
   GROUP BY    a.a_id

以上是有效的:

enter image description here

这是你的要求吗?

答案 1 :(得分:1)

可以使用correlated subquery

来完成
SELECT b_id AS ID,
       (SELECT COUNT(*)
        FROM TableA
        WHERE TableA.b_id = TableB.b_id
       ) AS count,
       b_name AS name
FROM TableB;