我正在尝试在 SQL Server 2008 中执行以下 SQL 查询
String query = "SELECT SUM(r.rate),COUNT(q.best_answer_id) " +
"FROM questions_rating r,questions q " +
"WHERE r.question_id IN (SELECT question_id FROM questions WHERE user_id = 1) "+
"AND q.best_answer_id IN (SELECT answer_id FROM answers WHERE user_id = 1)";
这些是表格
问题_评分
id---------question_id-----------user_id--------rate
1 1 1 1
2 1 2 1
3 1 3 -1
4 2 1 -1
比率只能是 1 或 -1 。
问题
question_id------question-------user_id-------best_answer_id
1 lala 1 3
2 lala 2 5
答案
answer_id---------answer--------user_id------question_id
1 lala 4 1
2 kaka 5 1
3 dada 6 1
4 fafa 7 2
5 tata 8 2
查询返回这些结果
SUM------COUNT
NULL 0
虽然它应该返回......
SUM------COUNT
1 0
请注意,以下查询返回正确的结果 (1)
String query = "SELECT SUM(r.rate) " +
"FROM questions_rating r " +
"WHERE r.question_id IN (SELECT question_id FROM questions WHERE user_id = 1) ";