我有这个问题:
我想像这样在图像表中插入新行:
INSERT INTO image (pr_id, kelias, tipas) VALUES(111111, CONCAT('1111111111', '_', image.id, '.jpg'), '0')
但是当我每次尝试输入CONCAT
值或每次返回0时,image.id
函数都会出错。
[image.id
是自动递增。
您可以使用BEFORE INSERT触发器-
CREATE TRIGGER trigger1
BEFORE INSERT
ON image
FOR EACH ROW
BEGIN
SET NEW.kelias = CONCAT(NEW.kelias, NEW.pr_id);
END
尝试
INSERT INTO image (pr_id, kelias, tipas)
SELECT 11111,
CONCAT('1111111111', '_', max(image.id) + 1, '.jpg'),
'0'
FROM image
mysql_query("INSERT INTO image (kelias, tipas) VALUES('anything', '0')");
$img_id = mysql_insert_id();
$kelias = '1111111111' . '_' . $img_id . '.jpg';
mysql_query("UPDATE image SET kelias='$kelias' WHERE pr_id = $img_id");
您可以使用BEFORE INSERT触发器-
CREATE TRIGGER trigger1
BEFORE INSERT
ON image
FOR EACH ROW
BEGIN
SET NEW.kelias = CONCAT( NEW.kelias , '_',
(SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "<your database name here>"
AND TABLE_NAME = "image" )
, '.jpg');
END
然后通过:]获得所需的结果>
INSERT INTO image (pr_id, kelias, tipas) VALUES(111111, '1111111111', '0')