本帖最后由 wuwei_longlong 于 2011-01-04 10:55:47 编辑

解决方案 »

  1.   

    比如 A,B,C 3张表,A 和 B的join 有10条记录,而A和C的记录只有1条,这里1就是小的结果集,去join B表;
      

  2.   

    把所有表的show index from tbname贴出来看看。
      

  3.   

    贴出你的SHOW INDEX TABLENAME;和EXPPALIN SELECT
      

  4.   

    begin 
      declare i int default 1;  
      DECLARE theNumOfZero2 INT ;
      DECLARE temp_ProductNum VARCHAR(10) DEFAULT "";
      
       while i<10000 do
       SET theNumOfZero2= 5-LENGTH(i);
       IF theNumOfZero2=1 THEN
          set temp_ProductNum= CONCAT("0",i);
          ELSEIF theNumOfZero2=2 THEN
          set temp_ProductNum= CONCAT("00",i);
          ELSEIF theNumOfZero2=3 THEN
          set temp_ProductNum= CONCAT("000",i); 
          ELSEIF theNumOfZero2=4 THEN
          set temp_ProductNum= CONCAT("0000",i); 
       END IF;
     
        set @OrderID1 = CONCAT('BS20101027',temp_ProductNum);
        
        INSERT order_list (OrderID,UserID,ContactEmail,OrderStatus,ShippingPrice,TotalPrice,PaymentCurrencyID,HasHardware,DeliveryStatus,ExpressTypeID,ShipperID,PrincipalID,CreateDate,Addr_Type,Insurance,Addr_Country,OrderType,SendMailStatus,PaymentTypeID) 
        VALUES 
        (@OrderID1,'BSU2010112900002','[email protected]',1,0.00,242.18,2,1,1,1,'bluesoleil','bluesoleil','2010-11-26 15:48:52','BlueSoleil Web',0,'CN',0,0,1);
         
        select @MaxID:=MAX(ID) from `order_list`;
        INSERT order_product_list(OrderID,ProductID,Quantity,Price) 
        VALUES 
        (@MaxID+1,'S0001201005190001',1,25.19);
        
        INSERT order_product_list(OrderID,ProductID,Quantity,Price) 
        VALUES 
        (@MaxID+1,'S0001201005190002',1,25.19);
        set i=i+1;
        end while; 
     end这个存储过程中select @MaxID:=MAX(ID) from `order_list`;为什么查询不出他的最大ID
      

  5.   

    begin 
      declare i int default 1;  
      DECLARE theNumOfZero2 INT ;
      DECLARE temp_ProductNum VARCHAR(10) DEFAULT "";
      
       while i<10000 do
       SET theNumOfZero2= 5-LENGTH(i);
       IF theNumOfZero2=1 THEN
          set temp_ProductNum= CONCAT("0",i);
          ELSEIF theNumOfZero2=2 THEN
          set temp_ProductNum= CONCAT("00",i);
          ELSEIF theNumOfZero2=3 THEN
          set temp_ProductNum= CONCAT("000",i); 
          ELSEIF theNumOfZero2=4 THEN
          set temp_ProductNum= CONCAT("0000",i); 
       END IF;
     
        set @OrderID1 = CONCAT('BS20101027',temp_ProductNum);
        
        INSERT order_list (OrderID,UserID,ContactEmail,OrderStatus,ShippingPrice,TotalPrice,PaymentCurrencyID,HasHardware,DeliveryStatus,ExpressTypeID,ShipperID,PrincipalID,CreateDate,Addr_Type,Insurance,Addr_Country,OrderType,SendMailStatus,PaymentTypeID) 
        VALUES 
        (@OrderID1,'BSU2010112900002','[email protected]',1,0.00,242.18,2,1,1,1,'bluesoleil','bluesoleil','2010-11-26 15:48:52','BlueSoleil Web',0,'CN',0,0,1);
         
        select @MaxID:=MAX(ID) from `order_list`;
        INSERT order_product_list(OrderID,ProductID,Quantity,Price) 
        VALUES 
        (@MaxID+1,'S0001201005190001',1,25.19);
        
        INSERT order_product_list(OrderID,ProductID,Quantity,Price) 
        VALUES 
        (@MaxID+1,'S0001201005190002',1,25.19);
        set i=i+1;
        end while; 
     end这个存储过程中select @MaxID:=MAX(ID) from `order_list`;为什么查询不出他的最大ID
      

  6.   

    select MAX(ID) into @MaxID from `order_list`
      

  7.   

    大部分情况下是这样,MYSQL会自动利用索引进行SQL语句的优化,但一些情况下还是需要人为的干预。
      

  8.   

    我查询出来的就是null,但是我单独查询的时候能查询出来.
      

  9.   

    select @MaxID:=MAX(ID) from `order_list`;
    select @MaxID;
    有无结果
      

  10.   

    你得到的结果是什么? 是不是有并发进程?因为你用的是一个共享的SESSION变量@MaxID
      

  11.   

    你的存储过程的完整代码是什么? 
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  12.   

    这单独查询是能查询出数据的.就是放到我刚才那个存储过程中的时候就只能查询出Null
      

  13.   


    select @MaxID:=MAX(ID) from `order_list`;
    select @MaxID;
    放入SP中运行,检查结果
      

  14.   

    在存储过程中运行这两句代码,mySql直接就没有响应了.等了半天结果看不到最后结果.只是数据还是插入成功了.应该是能查询出来的.!
      

  15.   


    我这数据库,关联的表太多了.比较复杂.如果这样写的话我估计大家都会看上半小时去了.我还是抓重点吧.
    我用的是MySql5
      

  16.   

    那就插入到临时表中,检查结果,建立LSB,1个字段
    select @MaxID:=MAX(ID) from `order_list`;
    insert into lsb values(@MaxID);
      

  17.   

    用临时表的话,能查询出数据.!:也就是说
    select @MaxID:=MAX(ID) from `order_list`;
    是取到MAX(ID)的值的
      

  18.   

    一是索引方面 ,EXPLAIN SQL语句
    二是表设计方面
      

  19.   

    程序实现部分尽量交给其他语言去实现,sql最好能精简查询