简体   繁体   中英

Use If exists for select statement in mysql

I am creating a view and written the below code snippet :

CREATE OR REPLACE VIEW vclPersonData
    AS
SELECT * FROM phone_data UNION 
SELECT * FROM Address 

I get an error if the table doesn't exists, to come overthat i used If Exists but it too doesn't works for me.

Any help is thankful. Thanks in Advance.

You'll need two steps in your script:

  1. CREATE TABLE IF NOT EXISTS
  2. CREATE VIEW AS SELECT * FROM TABLE

If the table exists, step 1 will be harmless. If the table does not exist, step 1 will create it and step 2 will create an empty view.

If you only want the view to be created IF the table exist, check the existance of the table before:

BEGIN
SELECT 1 FROM TABLE;
CREATE VIEW AS SELECT * FROM TABLE
COMMIT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM