Home > front end >  How to separate a SELECT query into multiple values in MySQL via Java
How to separate a SELECT query into multiple values in MySQL via Java

Time:12-07

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

CodePudding user response:

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 - ?
  • Related