Reputation: 115
I need to get sum of marks of level 4 and 5 from Results using a phone number as primary key .
The following way is the way it supposed to be but can not work with this i need to use case sql statement or anothe trick of sql if exists:
SELECT
SUM(marks)
FROM Results
WHERE phone=343435 AND level='Level 4' AND level='Level 5';
Upvotes: 0
Views: 52
Reputation: 11556
You could use an OR
instead of AND
like level='Level 4' OR level='Level 5'
.
Query
SELECT SUM(marks)
FROM Results
WHERE phone=343435 AND (level='Level 4' OR level='Level 5');
Or you can use an IN
operator.
Query
SELECT SUM(marks)
FROM Results
WHERE phone=343435 AND level IN('Level 4','Level 5');
Upvotes: 2
Reputation: 509
Try with an Or statement instead of and. You are saying the record's level field needs to have a value of 4 AND a value of 5. Instead of, give me rows where the VALUE =4 OR 5
SELECT
SUM(marks)
FROM Results
WHERE phone=343435 AND (level='Level 4' OR level='Level 5');
Upvotes: 0