0

Where is a problem because I try count values using sql queries:

(SELECT quantity FROM db WHERE no='998') this is fine

but (('500') - (SELECT quantity FROM db WHERE no='998')) // incorrect syntax near -

But I need to use constant 500. Where is problem

2
  • 1
    Don't put numbers in single quotes. Single quotes are only for character literals. Commented Nov 20, 2014 at 14:03
  • 1
    Why all the parenthesis? Is this statement part of a larger query? Commented Nov 20, 2014 at 14:06

4 Answers 4

2

SELECT 500-quantity FROM db WHERE no='998'

Sign up to request clarification or add additional context in comments.

Comments

1

How about this?

SELECT 500 - quantity
FROM db
WHERE no = 998;

A select statement needs to start with a select. In addition, numeric constants should not use single quotes (although that has no effect on whether the query parses or runs).

Comments

1

Use:

SELECT 500 - quantity FROM db WHERE no='998'
-- or if the no in the where clause is an integer and not a string:
-- SELECT 500 - quantity FROM db WHERE no=998

Or if you have to use a constant string literal:

SELECT '500' - quantity FROM db WHERE no='998'

in this case MySQL would implicitly convert it to a suitable integer anyway.

Comments

0
SELECT 500 - COUNT(quantity)
FROM db
WHERE no='998';

SQL query always begin by SELECT keyword.

Comments

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.