2013-02-19 66 views
1

我試圖將查詢的持續時間詳細信息記錄到表中。我遇到的問題是第8行。 這是因爲我需要從行2 & 3返回創建QUERY_ID的方法?從插入語句返回Query_ID

1)Set profiling =1; 

2)INSERT INTO Master 
3)VALUES ("[email protected]","efheif","pwoe"); 

4)INSERT INTO Duration(Status,DURATION) 
5)SELECT STATE, FORMAT(DURATION, 6) AS DURATION 
6)FROM INFORMATION_SCHEMA.PROFILING; 
7)SELECT * FROM INFORMATION_SCHEMA.PROFILING 
8)WHERE QUERY_ID = 1; 

感謝

+2

http://stackoverflow.com/questions/3837990/last-insert-id-mysql – ryan1234 2013-02-19 18:40:11

回答

0

試試這個。它是一個臨時變量,必須在插入或值重置後用於下一行。

設置分析= 1;

INSERT INTO Master 
VALUES ("[email protected]","efheif","pwoe"); 

SET @v1 = LAST_INSERT_ID() 

INSERT INTO Duration(Status,DURATION) 
SELECT STATE, FORMAT(DURATION, 6) AS DURATION 
FROM INFORMATION_SCHEMA.PROFILING; 
SELECT * FROM INFORMATION_SCHEMA.PROFILING 
WHERE QUERY_ID = @v1; 
+0

謝謝你只需要找出它爲什麼總是現在返回爲零。 – edited 2013-02-19 19:15:07

+0

您是否在違反列時將Query_Id設置爲自動增量? QUERY_ID mediumint NOT NULL AUTO INCREMENT – JParadiso 2013-02-19 19:24:54

+0

現在可用。AUTO INCREMENT未啓用。謝謝! – edited 2013-02-19 20:33:15

0

我猜你想返回插入第2行中值的標識?你可以通過使用獲取插入的Id;

SELECT LAST_INSERT_ID();

我將它分配給一個變量,然後在最後一個插入語句中使用它。

+0

我剛纔想SELECT LAST_INSERT_ID();但它返回值0.任何想法?謝謝 – edited 2013-02-19 18:58:28