-1

How do I replace non-existent records with not found in the following query. Right now I'm just getting the records that are found.

SELECT  CASE WHEN `Name` IS NOT NULL THEN `Name` ELSE 'Not Found' END AS Name
FROM    `studentDetails`
WHERE   `Transaction_ID` IN('496018490c1d5d60beb5', '77888f084c8a0e7578f5')

My input is

'496018490c1d5d60beb5',
'77888f084c8a0e7578f5'

What I'm getting is

Vinod Tonde

My desired output is

Not Found
Vinod Tonde 

The Database looks like enter image description here

Layers
  • 109
  • 9

4 Answers4

1

I think what you want is this:

SELECT  Transaction_ID, CASE WHEN count(*)>0 THEN `Name` ELSE 'Not Found' END AS Name
FROM    `studentDetails`
WHERE   `Transaction_ID` IN('496018490c1d5d60beb5', 'b6836a07a3c49af6187f')
group by Transaction_ID

OR, using this you could also try:

SELECT  IFNULL(Name, 'Not Found') AS Name
FROM (SELECT '496018490c1d5d60beb5' as col 
      union all 
      SELECT 'b6836a07a3c49af6187f') temp_table 
      LEFT JOIN studentDetails 
      ON temp_table.col=studentDetails.Transaction_ID collate utf8mb4_unicode_ci
Community
  • 1
  • 1
dev8080
  • 3,950
  • 1
  • 12
  • 18
0

Try this:

UPDATE `studentDetails` SET  `Name` = 'Not Found' WHERE (`Name` IS NULL OR `Name` = '') AND `Transaction_ID` IN('496018490c1d5d60beb5', 'b6836a07a3c49af6187f')
Biju P Dais
  • 131
  • 1
  • 12
  • No I don't want to Write to the database I just want to reformat the results to include "Not Found" when the record does not exist – Layers Apr 27 '17 at 09:13
0

Create a table for the ids to search:

CREATE TABLE ids (id VARCHAR(30));

Create a procedure to split a list of ids separated by a ',' and insert them into the ids table:

CREATE PROCEDURE split_id_list(IN input VARCHAR(300))
    BEGIN
    DECLARE tot_length int;
    DECLARE sub_length int;

    my_loop: LOOP

        SET tot_length = CHAR_LENGTH(input);

        INSERT INTO ids (id) VALUES(SUBSTRING_INDEX(input, ',', 1));

        SET sub_length = CHAR_LENGTH(SUBSTRING_INDEX(input, ',', 1))+2;
        SET input = MID(input, sub_length, tot_length);
            IF input = '' THEN
                LEAVE my_loop;
            END IF;
        END LOOP my_loop;
END;

Create a procedure to generate view containing the results:

CREATE PROCEDURE idsNames(IN id_list VARCHAR(500))
    BEGIN

    DECLARE a INT;
    DECLARE cur1 CURSOR for select count(*) FROM ids;

    OPEN cur1;
    FETCH cur1 into a;

    IF a > 0
         THEN DELETE FROM ids;
    END IF;

    CLOSE cur1;

    call split_id_list(id_list);

    CREATE OR REPLACE VIEW RESULTS(r_id,r_name) AS
    (SELECT ids.id, CASE WHEN Name IS NULL then 'NotFound' ELSE Name END
    FROM studentDetails 
    RIGHT JOIN ids
    ON studentDetails.Transaction_ID = ids.id); 

END;

Once the table and the procedures are created, each time you want to execute them, just execute the procedure with the required ids and a select from the view:

CALL idsNames('496018490c1d5d60beb5,b6836a07a3c49af6187f');
SELECT r_name FROM RESULTS;
Spock
  • 315
  • 2
  • 13
  • No! It's not working... still returns only the records that are found – Layers Apr 27 '17 at 09:39
  • I've tried before posting, with a table created for this purpose, containing just the column Name, with correct values, null values and empty values and it works. Are you sure that some names corresponding to the range of Transaction_ID for which you filter are empty or null? – Spock Apr 27 '17 at 09:43
  • No they don't exist in the database. – Layers Apr 27 '17 at 09:52
  • Sorry, I misanderstood your question, now I have updated my answer using procedures so that each time you want to search for some ids you can call the procedure idsNames with the ids separated by a ',', such as CALL idsNames('1,2,....,10') and select from the view. – Spock Apr 27 '17 at 15:03
0

You can add a few more alternatives to your case

SELECT  CASE
            WHEN Name IS NULL THEN 'Not Found'
            WHEN length(trim(Name)) = 0 THEN 'Not Found'
            ELSE Name
        END AS Name
FROM    studentDetails
WHERE   Transaction_ID IN('496018490c1d5d60beb5', 'b6836a07a3c49af6187f')

Edit

Since it seems that one of those two transaction_id doesn't exist in the database at all, you probably need something like this instead

select  coalesce(t2.Name, 'Not Found') as Name
from    (select '77888f084c8a0e7578f5' as trans union all select '496018490c1d5d60beb5') t1
left join
        studentDetails t2
on      t1.trans = t2.Transaction_ID
Stefano Zanini
  • 5,876
  • 2
  • 13
  • 33