1

I have a SQL table with a column that corresponds to a location and another that corresponds to a certain number , I also have an integer variable (let's call it n that corresponds to another number in my java code

I am using JDBC to query the server and what I need to do is the following: Apply the query: SELECT NUMBER FROM MYTABLE then for each number that the query outputs I want to check if n minus this number is <=3, my problem is that the select statement would generate multiple numbers, so how can I separate those? I am not sure if the solution would be Java related or MySQL related but any would work

1
  • Would you mind to share some code? Commented Dec 6, 2021 at 18:20

1 Answer 1

1

You you have an expression that involves a Java variable, but as far as the context of the SQL query, it's a constant value. That is, it's constant in the sense that it doesn't vary by row examined.

SELECT NUMBER, ? - NUMBER <= 3 AS `ExpressionSatisfied`
FROM MYTABLE

Use a prepared statement with a query parameter, and set the n variable as the parameter.

Then the result set, which consists of multiple rows, will have the NUMBER value and the result of whether it satisfied your intended condition.

Or you could run a query that returns only the rows where the condition is satisfied:

SELECT NUMBER
FROM MYTABLE
WHERE NUMBER >= 3 - ?
Sign up to request clarification or add additional context in comments.

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.