根据另一个 table 的值查询 table

Querying a table based on another table's value

所以这是我的应用程序的一般流程:

Main Menu -> Account Manager -> Account Details -> Payment Schedule

用户在Account Manager中选择的数据通过Serialized Object传递给Account Details。然后从 Account Details 将相同的 Serialized Object 传递给 Payment Schedule 用于计算目的,所有结果都被扔进我的数据库,特别是 PayTable.

每个帐户都可以有多次付款,因此为了确保我可以将正确的付款条目关联到正确的帐户,我在 PayTable 中有一个 GroupID。所以现在 PayTable 中的条目被分配了一个 GroupID,它基于传递给 Payment ScheduleAccountsTable 中的 AccountID

所以如果 AccountID = 1 那么 GroupID = 1.

使用它我可以过滤出正确帐户的正确付款,然后将它们显示在我的 GridView in Payments Schedule 中。

编辑(删除了大部分不再重要的旧 post,但将我的情况的要点放在首位以供参考)

总结

我有两个表 AccountPaymentAccountcolIDPaymentGroupIDGroupID 取决于所选帐户的 colID。因此,如果 colID 为 3,则该实例中的所有付款条目也将被分配为 3 的 GroupID,因此我可以将适当的付款与正确的帐户配对。

问题是我需要比较两个字段 (GroupID == colID ) 并获取所有符合条件的条目。所以这意味着对两个表中的两个字段进行交叉比较。

解决方案

  1. 使用了 INNER JOIN 并创建了一个视图以便于 GridView 参考:

    db.execSQL("CREATE VIEW " + viewPayments +
                    " AS SELECT " + payTable + "." + colPayID + " AS _id," +
                    " " + accountsTable + "." + colID + "," +
                    " " + payTable + "." + colGroupID + "," +
                    " " + payTable + "." + colPayBal + "," +
                    " " + payTable + "." + colInterest + "," +
                    " " + payTable + "." + colPayDue + "," +
                    " " + payTable + "." + colDateDue + "," +
                    " " + payTable + "." + colPaid + "" +
                    " FROM " + payTable +
                    " JOIN " + accountsTable + " ON " + payTable + "." + colGroupID + " = " + accountsTable + "." + colID ); 
    
  2. 修正了我的查询

    Cursor filterpay(String Payment) {
        SQLiteDatabase db = this.getWritableDatabase();
        String[] columns = new String[]{"_id", colGroupID, colPayBal, colInterest, colPayDue, colDateDue, colPaid};
        Cursor c = db.query(viewPayments, columns, colGroupID + "=?", new String[]{Payment}, null, null, null);
        return c;
    
  3. 最后但同样重要的是,在我的 GridView 上调整我的 posting 代码。

    TextView txt;
    txt = (TextView)findViewById(R.id.putithere);
    txt.setVisibility(View.GONE);
    
    public void Paygrid(){
    
    dbHelper = new DatabaseHelper(this);
    String Payment = String.valueOf(txt.getText());
    Cursor a = dbHelper.filterpay(Payment);
    startManagingCursor(a);
    
    String[] from = new String[]{DatabaseHelper.colPayBal, DatabaseHelper.colInterest, DatabaseHelper.colPayDue, DatabaseHelper.colDateDue, DatabaseHelper.colPaid};
    int[] to = new int[]{R.id.Amount,R.id.Interest, R.id.Total, R.id.DateDue, R.id.Paid};
    
    SimpleCursorAdapter saa = new SimpleCursorAdapter(this, R.layout.paygrid, a, from, to);
    intgrid.setAdapter(saa);
    

    }

txt 只是我懒得找到一种更简洁的方法来获取帐户的 colID 并在我的 'PayGrid' 中引用它所以我可能不会推荐在这方面关注我。

此外,由于我是新来的,所以当我回答自己的问题时,我不知道如何将此问题标记为已回答,所以如果有人能指出这一点,那就太好了。除此之外,我希望这对将来的人有所帮助。干杯。

顺便说一句,感谢随机投票的人!非常感谢您的帮助。

在我编辑并发布我自己的解决方案之后,我刚刚在页面底部看到了 "Answer your question" 按钮....是的,当我这样做的时候我做了 facepalm。

解决方案

  1. 使用 INNER JOIN 并创建了一个视图以便于 GridView 参考:

    db.execSQL("CREATE VIEW " + viewPayments +
                    " AS SELECT " + payTable + "." + colPayID + " AS _id," +
                    " " + accountsTable + "." + colID + "," +
                    " " + payTable + "." + colGroupID + "," +
                    " " + payTable + "." + colPayBal + "," +
                    " " + payTable + "." + colInterest + "," +
                    " " + payTable + "." + colPayDue + "," +
                    " " + payTable + "." + colDateDue + "," +
                    " " + payTable + "." + colPaid + "" +
                    " FROM " + payTable +
                    " JOIN " + accountsTable + " ON " + payTable + "." + colGroupID + " = " + accountsTable + "." + colID ); 
    
  2. 修正了我的查询

    Cursor filterpay(String Payment) {
        SQLiteDatabase db = this.getWritableDatabase();
        String[] columns = new String[]{"_id", colGroupID, colPayBal, colInterest, colPayDue, colDateDue, colPaid};
        Cursor c = db.query(viewPayments, columns, colGroupID + "=?", new String[]{Payment}, null, null, null);
        return c;
    
  3. 最后但同样重要的是,调整我的代码以在我的 GridView 上发布。

    TextView txt;
    txt = (TextView)findViewById(R.id.putithere);
    txt.setVisibility(View.GONE);
    
    public void Paygrid(){
    
    dbHelper = new DatabaseHelper(this);
    String Payment = String.valueOf(txt.getText());
    Cursor a = dbHelper.filterpay(Payment);
    startManagingCursor(a);
    
    String[] from = new String[]{DatabaseHelper.colPayBal, DatabaseHelper.colInterest, DatabaseHelper.colPayDue, DatabaseHelper.colDateDue, DatabaseHelper.colPaid};
    int[] to = new int[]{R.id.Amount,R.id.Interest, R.id.Total, R.id.DateDue, R.id.Paid};
    
    SimpleCursorAdapter saa = new SimpleCursorAdapter(this, R.layout.paygrid, a, from, to);
    intgrid.setAdapter(saa);
    

    }

txt 只是我懒得找到一种更简洁的方法来获取帐户的 colID 并在我的 PayGrid 中引用它所以我可能不会推荐在这方面关注我。