Search code examples
mysqlconcatenation

Mysql Create VIEW with CONCAT and two colums


I'm trying to create a view with two extra fields. I have this code that does the job with one extra filed

CREATE VIEW STRANKEPView AS (
    SELECT *, COALESCE(CONCAT('P-',strp_ID)) AS strp_nas_br
    FROM STRANKEP 
);

I need to add one more filed as

CONCAT('A',strp_aa, '.')) AS strp_aa_nas_br

I can not find example of this anywhere and what ever i tried is not working. How can I join this? Something along lines:

CREATE VIEW STRANKEPView AS (
    SELECT *, COALESCE(CONCAT('P-',strp_ID)) AS strp_nas_br 
    AND COALESCE(CONCAT('A',strp_aa,'.')) AS strp_aa_nas_br
    FROM STRANKEP 
);

Or I need possibility to alter the VIEW that I created with first statement. But when i do this i get the error that STRANKEPView is not a table

ALTER VIEW STRANKEPView AS (
    SELECT *, COALESCE(CONCAT('A',strp_aa,'.')) AS strp_aa_nas_br
    FROM STRANKEPView 
);

Solution

  • To change an existing view you use ALTER VIEW.

    The fields you're selecting are separated with ,, not AND.

    ALTER VIEW STRANKEPView AS (
        SELECT *, CONCAT('P-',strp_ID) AS strp_nas_br,
                  CONCAT('A',strp_aa,'.') AS strp_aa_nas_br
        FROM STRANKEP 
    );
    

    There's no point in using COALESCE() with just one argument, so I removed that from the view. The purpose of COALESCE() is to return an alternate value if the argument is NULL; it doesn't do anything useful if you don't supply the alternate value.