2012-10-27 89 views
-1

我歸我的資料庫,我試圖設置字段一樣的ID該行,所以我可以連接的表的值。字段的值設置爲ID值

我試着用LAST_INSERT_ID(),但它不工作。我沒有得到一個錯誤,但我只得到零。

INSERT INTO `eleves` (`user_id`, 
`first_name`, `last_name`, 
`username`, `groupe`, 
`password`, `courriel`, 
`active`, `auteur`, 
`citations`, `absurde`, 
`vocabulaire`, `analyse`, 
`themes`, `personnages`) 
VALUES (NULL, 
'Jane', 'Doe', 
'janedoe', '400', 
'password', '[email protected]', 
'1', LAST_INSERT_ID(), 
LAST_INSERT_ID(), LAST_INSERT_ID(), 
LAST_INSERT_ID(), LAST_INSERT_ID(), 
LAST_INSERT_ID(), LAST_INSERT_ID()); 

謝謝!

+0

你說的*空字段意味着*?據我所知,它應該插入一個零。 –

+0

我玩過它,現在變成零。但這仍然不是我想要插入的內容......我發佈了完整的代碼。 – Sarah

+0

什麼查詢會要求您在幾個字段中擁有相同的信息? –

回答

0

看來,圍繞你的MySQL會議期間,剛好有沒有自動生成的ID插入。因此對於要填寫last_insert_id()必須是zero但不是一個empty領域(NULL?SPACE?)。

嘗試在表中定義與自動增量主鍵,讓你產生一個新的記錄。看看會發生什麼!

請看下面的例子:

$ mysql -u ravi -p test 
Enter password: ******** 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 2 to server version: 5.0.22-community-nt 

Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 

mysql> select last_insert_id(); 
+------------------+ 
| last_insert_id() | 
+------------------+ 
|    0 | 
+------------------+ 
1 row in set (0.00 sec) 

mysql> create table eleves(user_id int not null auto_increment primary key, username varchar(10), auteur int, citations int, absurde int); 
Query OK, 0 rows affected (0.06 sec) 

mysql> insert into eleves(username, auteur, citations, absurde) 
    -> values('janedoe1', last_insert_id(), last_insert_id(), last_insert_id()); 
Query OK, 1 row affected (0.03 sec) 

mysql> select * from eleves; 
+---------+----------+--------+-----------+---------+ 
| user_id | username | auteur | citations | absurde | 
+---------+----------+--------+-----------+---------+ 
|  1 | janedoe1 |  0 |   0 |  0 | 
+---------+----------+--------+-----------+---------+ 
1 row in set (0.00 sec) 

mysql> select last_insert_id(); 
+------------------+ 
| last_insert_id() | 
+------------------+ 
|    1 | 
+------------------+ 
1 row in set (0.00 sec) 

mysql> insert into eleves(username, auteur, citations, absurde) 
    -> values('janedoe2', last_insert_id(), last_insert_id(), last_insert_id()); 
Query OK, 1 row affected (0.01 sec) 

mysql> select * from eleves; 
+---------+----------+--------+-----------+---------+ 
| user_id | username | auteur | citations | absurde | 
+---------+----------+--------+-----------+---------+ 
|  1 | janedoe1 |  0 |   0 |  0 | 
|  2 | janedoe2 |  1 |   1 |  1 | 
+---------+----------+--------+-----------+---------+ 
2 rows in set (0.00 sec) 

mysql> select last_insert_id(); 
+------------------+ 
| last_insert_id() | 
+------------------+ 
|    2 | 
+------------------+ 
1 row in set (0.00 sec) 

mysql> 
+0

仍然沒有工作...我放棄了,找到了另一種方式。不管怎麼說,多謝拉! – Sarah

0

刪除單引號他們

INSERT INTO `eleves`(`user_id`, `username`, `auteur`, `citations`, `absurde`) 
VALUES (NULL,'janedoe',LAST_INSERT_ID(),LAST_INSERT_ID(),LAST_INSERT_ID()); 
+0

是的,不好意思,我試着去執行它,但實際上我刪除了它們,但不知何故,我在發佈它時將它們放回去。編輯我的問題。它仍然不起作用。 – Sarah

+0

所以有什麼問題?它似乎在[SQLFiddle(點擊此處)](http://sqlfiddle.com/#!2/99e3b/1)上工作。 –

+0

不知何故,當我使用完整查詢時(我的表格更大),它不工作。 [鏈接](http://sqlfiddle.com/#!2/efc07) INSERT INTO'eleves'('user_id','first_name','last_name','username','groupe','password', 'courriel','active','auteur','citations','absurde','vocabulaire','analyse','themes','personnages')VALUES(NULL,'Jane','Doe','janedoe 」, '400', '密碼', '[email protected]', '1',LAST_INSERT_ID(),LAST_INSERT_ID(),LAST_INSERT_ID(),LAST_INSERT_ID(),LAST_INSERT_ID(),LAST_INSERT_ID(),LAST_INSERT_ID()) ; – Sarah