MySQL 如何判断一个临时表是否存在?

解决方案 »

  1.   

    create table if not exists table1( a int);
      

  2.   

    select count(*) from information_schema.tables where table_schema='your_schema' and table_name='your_tab'
      

  3.   

    5.1版本,
    下面是我写的存储过程
    CREATE DEFINER=`root`@`%` PROCEDURE `fine_tmp`(IN p_tablename varchar(50),OUT ISExist int)
    BEGIN
       declare cnt int;        
       declare stmt varchar(2000);   
       
       select count(1) from information_schema.tables where TABLE_SCHEMA='kungfu' and table_name=p_tablename  into @cnt;      
            #select p_tablename;
       if @cnt != 0 then    
          set @ISExist=1;      
              
       else    
         set @ISExist=0;          
            end if;   
    select @ISExist;
    END;@cnt总是为0,但是其实临时表是存在的
    这是调用语句 :call fine_tmp('tmp_123',@cnt)  
      

  4.   

    测试了一下,SP没有问题,你的表是用
    CREATE TEMPORARY TABLE ....建立 的?
      

  5.   

    mysql的临时表,应该是session 相关的吧 。你的这个临时表,不是自己这个连接建立的吧 ?
      

  6.   

    我要现实的功能是检查某一个临时表是否存在,如果不存在,就创建一个,并且返回0,如果已经存在就直接返回1,我是这样显示的,但是临时@cnt一直都是0,其实该临时表已经存在,我是在MySQL——front中写的,应该是同一个连接,但是我最终的目的是,通过临时表控制重复登陆,不同的连接不能检测到临时表码???CREATE DEFINER=`root`@`%` PROCEDURE `fine_tmp`(IN p_tablename varchar(50),OUT ISExist int)
    BEGIN
       declare cnt int default 0;        
       declare stmt varchar(2000);   
      select count(*) from information_schema.tables where table_name=p_tablename into @cnt;  
        select @cnt;
       if @cnt != 0 then    
          set @ISExist=1;      
              
       else    
         set @ISExist=0;          
         set @m_sql=concat('create TEMPORARY table ',p_tablename,'(id int(11))');
         select @m_sql;     
         prepare stmt from @m_sql;
         execute stmt;
       end if;   
    select @ISExist;
    END;
      

  7.   

    不同的连接不能检测到临时表码
    不能,你是要检测同一个临时表吧,是session级的。
    MYSQL HELP:
    You can use the TEMPORARY keyword when creating a table. A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed. This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.) 
      

  8.   

    感谢wwwwb,我刚才测试了一下,确实像你说的不同的连接检测不到临时表,请教一下有没有什么好的办法实现我上面设想的功能呢?就是控制重复登陆
      

  9.   

    用一个物理表保存已经登陆的ID,登陆时先检查此表中ID状态(已经登陆 OR 未登陆),再做处理
      

  10.   

    如果用物理表的话,会出现这样的问题,比如用账户ID1登录了之后,写入登陆状态,那么如果该登录电脑断电了之后,账户ID1的状态就会一直处于登陆状态下,账户ID1就不能再用了。
      

  11.   

    我就是考虑到这个才用临时表,但是看来,MySQL中临时表是现实不了了,不知道你还有什么其他好的方法没??
      

  12.   

    在断电情况下,除了UPS外,没有其它方法
      

  13.   

    你想实现的是控制一个账号只允许在同时一个登录? 这个应该可以在MYSQL直接设置。
      

  14.   

    你想实现的是控制一个账号只允许在同时一个登录? 这个应该可以在MYSQL直接设置。
      

  15.   

    你可以直接设置该用户账号的 MAX_USER_CONNECTIONS = 1 就可以了。
    mysql> CREATE USER 'francis'@'localhost' IDENTIFIED BY 'frank';
    mysql> GRANT ALL ON customer.* TO 'francis'@'localhost'
        ->     WITH MAX_QUERIES_PER_HOUR 20
        ->          MAX_UPDATES_PER_HOUR 10
        ->          MAX_CONNECTIONS_PER_HOUR 5
        ->          MAX_USER_CONNECTIONS 2;