mysql里怎么用一条sql语句判断某个字段是否存在并且如果不存在的话添加这个字段?mysql 里的判断不熟悉.

解决方案 »

  1.   

    不能吧,为什么要用一条sql语句?
      

  2.   

    试一下!
    if not exists (
    select * from syscolumns where name='字段名' 
    and [id]=(select [id] from sysobjects where name='表名')
    )
    begin
         alter table [表名] add columns.........
    end
      

  3.   

    You can not only use a single statement to do this.
    The store procedure within mysql can satisfy your demand.
    For example.
    Tablename :t
    Columnname:idDELIMITER $$CREATE PROCEDURE `t1`.`sp_t`()
    BEGIN
    if not exists (select 1 from t where id = 2) then
      insert into t(id) values (2);
    else
      begin end;
    end if;
    END$$DELIMITER ;
      

  4.   

    我用if not exist语句是出现错误了,不知道为啥……