1

I have a MySQL query to produce a view which displays two columns, the two columns are sub query's but for some reason I am getting error.

SELECT 
(SUM(cards_required) FROM `orders` WHERE statuses = 1) As Count,
(SUM(cards_required) FROM `orders` WHERE statuses = 2) As Count
FROM `orders` WHERE user_id = 1

error message

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM orders WHERE statuses = 1) As Count, (SUM(cards_required) FROM orders ' at line 3

4
  • 1
    dont see any error message Commented Sep 28, 2015 at 14:02
  • That's what I thought @amdixon Commented Sep 28, 2015 at 14:03
  • #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM orders WHERE statuses = 1) As Count, (SUM(cards_required) FROM orders ' at line 3 Commented Sep 28, 2015 at 14:03
  • ok youve got multiple where clauses and from clauses - why ? Commented Sep 28, 2015 at 14:04

1 Answer 1

1

The syntax you're using isn't quite right. You can use IF to conditionally sum:

SELECT 
SUM(IF(statuses = 1,cards_required,0)) As status_1,
SUM(IF(statuses = 2,cards_required,0)) As status_2
FROM `orders` WHERE user_id = 1
Sign up to request clarification or add additional context in comments.

1 Comment

Wow that worked! I usually work in SQL seems there are more differences than I thought!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.