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

时间:2016-04-22 19:09:48

标签: android sqlite android-sqlite

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

的所有赛程

我有以下查询,它仅返回idteam_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 = ?

OR

WHERE fixtures.team_2_id = ?

查询会返回相关结果,但我希望能够看到idteam_1_id

中是否显示team_2_id

任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:3)

作为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 ...

答案 1 :(得分:1)

您可以使用in

WHERE ? in (fixtures.team_1_id, fixtures.team_2_id)