将 OR 运算符与 2 个具有相同值的占位符一起使用
Using OR operator with 2 placeholders with same value
我是 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 = ?
查询returns相关结果,但我希望能够看到id
是否出现在team_1_id
或team_2_id
中
如有任何帮助,我们将不胜感激。
您可以使用 in
:
WHERE ? in (fixtures.team_1_id, fixtures.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 ...
我是 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 = ?
查询returns相关结果,但我希望能够看到id
是否出现在team_1_id
或team_2_id
如有任何帮助,我们将不胜感激。
您可以使用 in
:
WHERE ? in (fixtures.team_1_id, fixtures.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 ...