我正在尝试以下列格式为我的表创建自定义ID
2个随机字母 - 00MaxID
例如:AA-001
我曾尝试为它编写查询,但它无法正常工作,这是我第一次编写触发器也编写了一个复杂的查询,如此。
更新的-2
以下查询给出了“SELECT count(cus_id)INTO @ct FROM customer;”附近的错误。
CREATE
TRIGGER `id_gen` BEFORE INSERT
ON `testdb`.`customer`
FOR EACH ROW BEGIN
SELECT count(cus_id) INTO @ct FROM customer;
IF @ct < 1000 THEN
SET @cs_id = LPAD(@ct+1, 3, 0 );
ELSE
SET @cs_id = @ct+1;
END IF;
SET NEW.cus_id = CONCAT(CHAR(FLOOR(65 + RAND() * 26),FLOOR(65 + RAND() * 26)),'-',@cs_id);
END;
错误
SQL query: Documentation
CREATE
TRIGGER `id_gen` BEFORE INSERT
ON `testdb`.`customer`
FOR EACH ROW BEGIN
SELECT count(cus_id) INTO @ct FROM customer
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 6
答案 0 :(得分:0)
@Solarflare回答。
在屏幕截图中,您可以看到phpmyadmin自动添加了包含for each row
的代码,该代码现在在语句中两次(因此错误)。您自己的代码将以begin
开头。
BEGIN
SELECT count(cus_id)+1 INTO @ct FROM customer;
IF @ct < 1000 THEN
SET @cs_id = LPAD(@ct, 3, 0 );
ELSE
SET @cs_id = @ct;
END IF;
SET NEW.cus_id = CONCAT(CHAR(FLOOR(65 + RAND() * 26),FLOOR(65 + RAND() * 26)),'-',@cs_id);
END;