android – 使用OR运算符和2个具有相同值的占位符

我是Android编程的新手,并寻求我的查询的一些帮助.我正在尝试查找所选球队的ID等于主队team_1_id或客队team_2_id的所有赛程

我有以下查询,它只返回id为team_1_id而不是team_2_id的结果

public List < Fixture > getTeamFixtures(int id) {
 List < Fixture > fixtureList = new ArrayList < Fixture > ();

 // Select All Query
 String selectQuery = "
SELECT fixtures.id, 
       fixtures.team_1_id, 
       fixtures.team_2_id, 
       fixtures.date, 
       fixtures.time, 
       fixtures.pitch, 
       teams.team_name, 
       teams_1.team_name AS awayTeam, 
       leagues.league_name, 
       leagues.id 
FROM   fixtures 
       INNER JOIN teams 
               ON fixtures.team_1_id = teams.id 
       INNER JOIN teams AS teams_1 
               ON fixtures.team_2_id = teams_1.id 
       INNER JOIN teams_vs_leagues 
               ON teams.id = teams_vs_leagues.team_id 
       INNER JOIN leagues 
               ON teams_vs_leagues.league_id = leagues.id 
WHERE  fixtures.team_1_id = ? 
        OR fixtures.team_2_id = ? 
";

 sqliteDatabase db = this.getWritableDatabase();
 Cursor cursor = db.rawQuery(selectQuery, new String[] {
  String.valueOf(id)
 });
 // looping through all rows and adding to list
 if (cursor.movetoFirst()) {
  do {
   Fixture fixture = new Fixture();
   fixture.setId(Integer.parseInt(cursor.getString(0)));
   fixture.setTeamId1(Integer.parseInt(cursor.getString(1)));
   fixture.setTeamId2(Integer.parseInt(cursor.getString(2)));
   fixture.setDate(cursor.getString(3));
   fixture.setTime(cursor.getString(4));
   fixture.setPitch(cursor.getString(5));
   fixture.setTeamName1(cursor.getString(6));
   fixture.setTeamName2(cursor.getString(7));
   fixture.setLeagueName(cursor.getString(8));
   fixture.setLeagueId(Integer.parseInt(cursor.getString(9)));
   // Adding team to list
   fixtureList.add(fixture);
  } while (cursor.movetoNext());
 }
 // close inserting data from database
 db.close();
 // return fixturelist
 return fixtureList;

}

每当我尝试

WHERE fixtures.team_1_id = ?

要么

WHERE fixtures.team_2_id = ?

查询会返回相关结果,但我希望能够查看该ID是出现在team_1_id还是team_2_id中

任何帮助将非常感谢.

解决方法:

作为documented,您可以通过为其指定显式数字来重用参数:

... WHERE fixtures.team_1_id = ?1 OR fixtures.team_2_id = ?1 ...

或者给它一个名字:

... WHERE fixtures.team_1_id = :TeamID OR fixtures.team_2_id = :TeamID ...

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能