1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
|
DROP TABLE IF EXISTS `test_table`;
CREATE TABLE `test_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`type` enum('First','Second','Third','Last') DEFAULT NULL,
`num` int(11) NOT NULL DEFAULT 0,
`price` decimal(10,2) NOT NULL DEFAULT 0.00,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB;
INSERT INTO `test_table` (`id`,`name`,`type`,`num`,`price`) VALUES
(1,'Art 1','First',1,10.50),
(2,'Art 2','Second',3,12.50),
(3,'Art 3','Second',2,15.50);
select last_insert_id(); /* returns 0, No Value */
truncate `test_table`;
INSERT INTO `test_table` (`name`,`type`,`num`,`price`) VALUES
('Art 1','First',1,10.50),
('Art 2','Second',3,12.50),
('Art 3','Second',2,15.50);
select last_insert_id(); /* returns 1, First Value */
select * from `test_table`;
INSERT INTO `test_table` (`name`,`type`,`num`,`price`) VALUES
('Art 4','Last',1,10.50);
select last_insert_id(); /* returns 4, Last Value */
select * from `test_table`;
select last_insert_id(12); /* returns 12 */
select last_insert_id(); /* returns 12 */
INSERT INTO `test_table` (`name`,`type`,`num`,`price`) VALUES
('Art 5','Last',1,10.50);
select last_insert_id(); /* returns 5, Last Value */
|