sqlite选择尴尬的行为

时间:2017-05-19 09:10:47

标签: c# wpf sqlite

我使用sqlite.NET(c#) 当我执行这个陈述时

"SELECT id, date, team1, team2, finalScore, winner FROM matches WHERE team1 = @team OR team2 = @team AND date BETWEEN @fromDate AND @toDate ORDER BY date DESC"

忽略"team2 = @team",仅选择"team1 = @team"。 如果我删除"OR team2 = @team"而不是返回查询是空的,而它不应该。 如果我将"team1 = @team OR team2 = @team"放入括号中,则返回空查询

这里是所有代码

public System.Data.DataTable searchMatches(string team, string fromDate, string toDate)
{
    SQLiteCommand cm = new SQLiteCommand("SELECT id, date, team1, team2, finalScore, winner FROM matches WHERE team1 = @team OR team2 = @team AND date BETWEEN @fromDate AND @toDate ORDER BY date DESC", cnlite);
    cm.Parameters.AddWithValue("@team", team);
    cm.Parameters.AddWithValue("@fromDate", fromDate);
    cm.Parameters.AddWithValue("@toDate", toDate);
    SQLiteDataAdapter sda = new SQLiteDataAdapter(cm);

    System.Data.DataTable table = new System.Data.DataTable();
    sda.Fill(table);
    return table;
}
DataTable matches = db.searchMatches(sheetDataXlsx.Rows[i]["Ομαδα 1"].ToString(), matchDate.ToString("yyyy/MM/dd"),
                                                    matchDate.AddDays(-90).ToString("yyyy/MM/dd"));

PS日期是一个字符串(" yyyy / MM / dd")

1 个答案:

答案 0 :(得分:1)

documentation中所述,AND的优先级高于OR。 所以你的查询相当于:

SELECT ... WHERE team1 = ? OR (team2 = ? AND date BETWEEN ...) ...

您必须手动添加括号以强制执行正确的评估顺序。