所以我想获得该项目的ID。如果它不存在,则插入它并使用LAST_INSERT_ID()
返回它。所以不管它是否存在,我最后都会有一个id。
在这里找到答案,即使是sql-server:stackoverflow.com/a/8030455/6554121
以下是我通过创建程序尝试的内容如下:
CREATE PROCEDURE `find_map_items` (OUT o INT, IN b TEXT, IN n TEXT, IN q TEXT, IN m TEXT, IN t TEXT)
BEGIN
IF NOT EXISTS(
SELECT id
FROM item
WHERE
brand = b
AND
name = n
AND
quantity = q
AND
measurement = m
AND
type = t
)
BEGIN
INSERT INTO item (brand, name, quantity, measurement, type)
VALUES (b, n, q, m ,t);
SELECT LAST_INSERT_ID() INTO o
END
ELSE
BEGIN
SELECT id
FROM item
WHERE
brand = b
AND
name = n
AND
quantity = q
AND
measurement = m
AND
type = t
END
END
但它给了我一些红色文字,我不确定问题是什么:
我正在使用MySQL Workbench。