Home > other >  Comparing primary Id of the same row in same table
Comparing primary Id of the same row in same table

Time:12-29

Can't find the same questions as mine I'm hoping you guys can help me. I have this query for my SP what I need to do is check if the company Id is the same based on the parameter given by the user.

I have two parameters which are paramOrgA and paramOrgB which will be supplied by the user. What I did was repeat the same query and set the returned value in two variables which is orgAId and orgBId then use the IF statement to compare the value.

is there another way to accomplish this or should I say optimized way to do this?

Below is the query I wrote.

BEGIN
declare orgAId int;
declare orgBId int;
declare orgStatus bool;

    SET orgAId = (SELECT c.Id
        FROM Members as `m`
        INNER JOIN Company as `c`
        ON m.CompanyId = c.Id
        WHERE m.Id = paramOrgA);
        
    SET orgBId = (SELECT c.Id
        FROM Members as `m`
        INNER JOIN Company as `c`
        ON m.CompanyId = c.Id
        WHERE m.Id = paramOrgB);
    
        
        IF (orgAId = orgBId) THEN
            set orgStatus = true;
        ELSE
            set orgStatus = false;
        END IF;
select orgStatus as 'CompanyStatus';
END IF;
END

CodePudding user response:

One query will do

SELECT count(distinct c.Id) > 1 as 'CompanyStatus'
FROM Members as `m`
INNER JOIN Company as `c` ON m.CompanyId = c.Id
WHERE m.Id IN (paramOrgA, paramOrgB)
  • Related