2010-12-03 78 views

回答

2

你可以用」減少查詢的數量 - 他們都做不同的事情 - 但你可以減少e數據庫的往返次數以及通過將所有數據包裝爲PLSQL函數進行解析的次數。

但是你無法選擇你的數據已經刪除後.....但考慮:

CREATE PROCEDURE s_u_d(a) 
BEGIN 

UPDATE tab_x SET tab_x.avalue=1 WHERE tab_x.another=a; 

DELETE FROM tab_y WHERE tab_y.avalue=a; 

SELECT * 
FROM tab_x 
WHERE tab_x.another=a; 

END; 

NB - 你也可以在同一個程序運行多個選擇和處理多個不同整形結果集,例如see this page

1

NO
只能結合

  • 項,然後選擇
  • UPDATE和SELECT

這不是MySQL的優化只是
有道因爲每個查詢來與不同的查詢成本。

而且在MyISAM中,它涉及到表級鎖寫

舉例UPDATE和SELECT

/* this will update TABLE_A if ID in TABLE_B exist in TABLE_A */ 
UPDATE TABLE_A, TABLE_B 
    SET TABLE_A.SOME_COLUMN=TABLE_B.SOME_COLUMN 
WHERE TABLE_A.ID=TABLE_B.ID 

/* or */ 
UPDATE TABLE_A 
    SET SOME_COLUMN = (SELECT SOME_COLUMN_B FROM TABLE_B WHERE ... LIMIT 1) 

舉例DELETE和SELECT

DELETE FROM TABLE_A WHERE TABLE_A IN(SELECT ID FROM TABLE_B) 
+1

能否請你告訴我SELECT和UPDATE例如3個查詢? – 2010-12-03 09:46:02

0

創建一個存儲過程:

DELIMITER // 

create procedure empproc(in name varchar(255),in fathername varchar(255),in password varchar(255)) 

begin 

     Select * from xemp where uname = name and fname = fathername; 

     insert into xemp values(name,fathername,password); 

end // 
delimiter ; 

Java代碼的.....

import java.sql.*; 

public class StoredProcedure { 

    public static void main(String a[])throws Exception { 

     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
     Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/01jim2010","root",""); 
     CallableStatement calstat=conn.prepareCall("{call empproc(?,?,?)}"); 
     calstat.setString(1,"Jimit"); 
     calstat.setString(2,"Temp"); 
     calstat.setString(3,"Temp"); 

     ResultSet rs = calstat.executeQuery(); 
     conn.close(); 
     calstat.close(); 

     System.out.println("Your data has been inserted into table."); 
     } 
} 
相關問題