Skip to content Skip to sidebar Skip to footer

Mysql Tinybit(1) Column Through A View

I have a view that joins 2 tables. One of the tables has a column of type tinyint(1) representing a boolean value. This table does not always have an entry when joining, so the vie

Solution 1:

https://www.db-fiddle.com/f/igDh3aJEXRLZEkL7eNrcnc/0

CREATEFUNCTION get_tinyint(MY_BOOLEAN tinyint) RETURNS TINYINT(1) 
RETURNCOALESCE(MY_BOOLEAN, 0);

CREATEOR REPLACE VIEW TEST_VIEW ASSELECT 
    T1.ID AS ID,
    get_tinyint(T2.MY_BOOLEAN) AS MY_BOOLEAN
FROM
    TEST1 T1 LEFTJOIN TEST2 T2 ON T1.ID=T2.TEST1_ID;

Solution 2:

You could use the null-save equal operator

CREATE OR REPLACE VIEW TEST_VIEW
ASSELECT 
        T1.ID AS ID,
        T2.MY_BOOLEAN <=> 1AS MY_BOOLEAN
    FROM
        TEST1 T1 LEFT JOIN TEST2 T2 ON T1.ID=T2.TEST1_ID;

Demo

Post a Comment for "Mysql Tinybit(1) Column Through A View"