Check if a record doesn't exist OR if a condition is true, return the parameter in MySQL

115
September 12, 2018, at 11:00 AM

Given a simple table like this:

var1 | var2
  0  |  3
  2  |  4
  6  |  5

I would need to return the lowest value (var1, as a multiple of 2) for which either a record does not exist (in this case, 4) or var2 is equal to 5 (in this case, 6). I'd need to do it as a single query, but unfortunately I'm new to MySQL functions.

I tried to create a function like this:

DELIMITER //
CREATE FUNCTION Prova ( starting_value INT ) returns int;
BEGIN
   DECLARE testNum INT DEFAULT 0;
  test_loop : LOOP
    IF (SELECT db.tabella WHERE var1 = testNum AND var2 <= 5) OR WHERE NOT EXISTS (
    SELECT * FROM db.tabella WHERE var1 = 'testNum'
) LIMIT 1; THEN
    RETURN testNum;
    END IF;
SET testNum = testNum + 2;
SELECT testNum; 
END LOOP; 
END;

but it doesn't seem to work. Thank you.

Rent Charter Buses Company
READ ALSO
Mysql optimization, how to get limit working after Join | location based selection

Mysql optimization, how to get limit working after Join | location based selection

The following shortened query select all rows (entrys) inside a given distance (calculated from user ealtloc = 0: location or e

111
Retrieving data from MySQL database using NodeJS and passing it to AngularJS page

Retrieving data from MySQL database using NodeJS and passing it to AngularJS page

I've searched and searched for a clear answer but can't seem to find itI'm "new" to programming, at least when it's about AngularJS and NodeJS (base languages like HTML, CSS and plain JS I'm familiar with because of school)

112
Access MySql with Entity Framework Core

Access MySql with Entity Framework Core

I try to connect my MySQL database to the test code of the ASPNET Core (v2

231
Is it possible to copy data directly from MySql Local to AWS RDS Cluster Aurora Serverless

Is it possible to copy data directly from MySql Local to AWS RDS Cluster Aurora Serverless

i have a database that i want to move to AWS RDS Amazon Aurora Serverless, i dont have an instance i only have a cluster that i have created which is MySQL Aurora serverless, so is it possible to do a dump from MySQL directly to the cluster instead of dumping...

128