MySQL error code when trying to create a function that checks data?

rookiecookie

So I have two tables in my database that contain a member id. I'm trying to create a stored function that returns 0 when a member is NOT in table A but IS in table B.

Here's my code:

Delimiter //
CREATE FUNCTION DoesMemberExist ( mid char(10))
RETURNS INTEGER
READS SQL DATA
BEGIN
IF(((SELECT COUNT(*) 
     FROM members 
     WHERE member_id = mid) = 0) AND ((SELECT COUNT(*) 
                                       FROM rent_equipment_log
                                       WHERE member_id = mid) > 0)) THEN
RETURN 0;
ELSE RETURN 1;
END IF;
END;

The error I'm getting is 1064 and it's saying there's a problem with my syntax but I can't figure out whats wrong? Thanks in advance!

Madhur Bhaiya

You can utilize EXISTS() instead. Also, END IF was missing to end the IF clause.

DELIMITER //
CREATE FUNCTION DoesMemberExist ( mid char(10))
RETURNS INTEGER
READS SQL DATA
BEGIN
  IF( NOT EXISTS(SELECT 1 FROM members 
                 WHERE member_id = mid) 
      AND 
      EXISTS(SELECT 1 FROM rent_equipment_log 
             WHERE member_id = mid) ) THEN 
    RETURN 0;
  ELSE RETURN 1;
  END IF;   -- We need to END the IF clause
END //
DELIMITER ;   -- Redefine the delimiter back to ;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mysql Error 1064 when trying to create a table

Syntax error when trying to create a procedure in mysql

Error when trying to create a Data.List?

Get error when trying to insert data into MySQL

Error when trying to insert data to MySQL

MySQL Error 1064 when create function

Getting a "not a function" error when trying to create a Signin with JWT

Syntax error near RECURSIVE when trying to create a procedure in mysql

Getting a Syntax Error when Trying to Create a CTE in MySQL

Getting error 1822 in MySQL when trying to create a foreign key

MySQL error when trying to create many to many relationship

mysql error 1822 when trying to create new table with a FOREIGN KEY

trying to write a basic echo function that also checks a mysql table

MySQL Error Code 1175 when trying to update inside stored procedure

MySQL error code 1093 when trying to delete rows

Getting an error when trying to create data using Angular

JSON code error 'Extra Data' when trying to dump/load a dictionary

Error Appears When Trying to Display MySQL Table Data in HTML Table

Getting MySQL Error #1064 when trying to insert data

Error when trying to create a table

Error When trying the urldownloadtofile function

"Unable to prove literally" error when trying to create a symbolic function based on an existing function

Problems when trying to create a QR code encoder

Error trying to create a SQL Server 2014 function

Getting "Request failed with status code 401" error when trying to create a playlist in Spotify using their API

MySQL error while trying to create table,

MySQL error while trying to create tables

2 problems when trying to create a trigger in MySQL

FIREBASE and React Native ERROR when trying to create a new node with child (then is not a function)