简体   繁体   English

在mysql中使用If exists for select语句

[英]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. 如果表不存在,我会收到错误,如果存在,我会使用If Ifists但它也不适用于我。

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. 如果表存在,则步骤1将是无害的。 If the table does not exist, step 1 will create it and step 2 will create an empty view. 如果表不存在,则步骤1将创建它,步骤2将创建一个空视图。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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