0

我需要有关 My Sql 查询的帮助..

我的表是这样的

_id studid  recid   arrival
834 29     436      12:04
835 29     437      12:19
836 29     438      12:35
837 29     439      12:43

现在我需要比较recid 436recid 439

正是这样

  _id(recid(436)) < _id(recid(439)) // 834 < 837 (true)

我需要查询这个..

我试过这样..

public boolean Check(String tid,String src,String dest)
{
    Cursor c1 = this.db.query(TABLE_NAME_2, new String[] { "_id" },
            "_studId = \"" + src +"\"  AND _recId = \"" + tid+"\"" , null, null, null, null);

    Cursor c2 = this.db.query(TABLE_NAME_2, new String[] { "_id" },
            "_studId = \"" + dest +"\"  AND _recId = \"" + tid+"\"" , null, null, null, null);
    int srcid = 0;
    int destid = 0;

    if (c1.moveToFirst()) 
    {
          do {
              srcid = Integer.parseInt(c1.getString(0));
           } while (c1.moveToNext());
    }
    if (c1 != null && !c1.isClosed()) {
         c1.close();
    }
    if (c2.moveToFirst()) 
    {
        do {
            destid = Integer.parseInt(c2.getString(0));
        } while (c2.moveToNext());
    }
    if (c2 != null && !c2.isClosed()) {
        c1.close();
    }   
    if(srcid < destid)
    {
        return true;
    }
    else
    {
        return false;
    }
}

像这样检查时返回正确的结果

 System.out.println(dh.Check("29", "436", "439")); // returns true.

但我认为它的程序不正确,因为会有多个记录。所以如果有 50 条记录,那么我必须执行这个函数 50 次。所以我需要一个简单的查询。

如果任何人有这样的经历。请帮帮我..

提前致谢。

4

1 回答 1

1

您可以使用这样的查询,它返回 0 或 1:

SELECT (SELECT _id FROM MyTable WHERE recid = 436) <
       (SELECT _id FROM MyTable WHERE recid = 439)

为了使重复执行更有效,请使用预编译语句,您可以使用不同的参数多次执行该语句:

SQLiteStatement stmt = db.compileStatement(
    "SELECT (SELECT _id FROM MyTable WHERE studid = ? AND recid = ?) < "+
           "(SELECT _id FROM MyTable WHERE studid = ? AND recid = ?)");
...
int recid_src = 436;
int recid_dst = 439;
int studid = 29;
stmt.bindLong(1, studid);
stmt.bindLong(2, recid_src);
stmt.bindLong(3, studid);
stmt.bindLong(4, recid_dst);
boolean result = stmt.simpleQueryForLong() != 0;
于 2013-01-24T11:21:10.940 回答