我需要一个关于My Sql Query的帮助..
我的表就像这样
_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 436
和recid 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次。所以我需要一个简单的查询..
如果有任何身体有这样的经历。请帮帮我..
先谢谢。
答案 0 :(得分: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;