, 당신은이 같은 트리거를 사용할 수 있습니다
CREATE TRIGGER `your_table_before_ins_trig` BEFORE INSERT ON `your_table`
FOR EACH ROW
begin
declare next_id int unsigned default 1;
-- get the next ID for your Account Number
select max(ID) + 1 into next_id from your_table where Account = new.Account;
-- if there is no Account number yet, set the ID to 1 by default
IF next_id IS NULL THEN SET next_id = 1; END IF;
set new.ID= next_id;
end#
참고! delimiter 컬럼은 # 위의 SQL 문에 있습니다!
이 같은 모든 AUTO_INCREMENT 기능없이 만들 경우이 솔루션은 귀사와 같은 테이블에 대해 작동합니다
이
CREATE TABLE IF NOT EXISTS `your_table` (
`ID` int(11) NOT NULL,
`Account` int(11) NOT NULL,
PRIMARY KEY (`ID`,`Account`)
);
이제이처럼 값을 삽입 할 수 있습니다 : 그것은에
INSERT INTO your_table (`Account`) VALUES (1);
INSERT INTO your_table (`Account`, `ID`) VALUES (1, 5);
INSERT INTO your_table (`Account`) VALUES (2);
INSERT INTO your_table (`Account`, `ID`) VALUES (3, 10205);
됩니다 이 :
ID | Account
------------------
1 | 1
2 | 1
1 | 2
1 | 3
출처
2014-05-05 13:09:09
Pat
무엇이 엔진을 사용하고 있습니까? – Mark