字段列表中的MySQL未知列。是由触发器引起的?

时间:2018-06-21 18:10:09

标签: mysql

这使我怀疑我对MySQL的了解,但这使我发疯。

这是一个真正的简单错误:

Error
SQL query: 

INSERT INTO upplysingar (ID, post_id, meta_key, meta_value) 
VALUES 
('', '1', 'nem_nafn', 'Bergur'), 
('', '1', 'nem_email', 'bergur@togg.is'), 
('', '1', 'nem_kennitala', '0411932639'), 
('', '1', 'asdfgfsad', 'sadfg'), 
('', '1', 'sad', 'asdsfsfgssda')

MySQL said:  
#1054 - Unknown column 'meta_value' in 'field list'

我要向其中插入数据的表是:

CREATE TABLE upplysingar
(
    id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
    post_id int,
    meta_key varchar(50),
    meta_value varchar(50)
);

据我所知,列名没有拼写错误,并且配额都正确。我在第一列(id)中尝试使用“ NULL”而不是“”。我没有主意,当我使用phpmyadmin上的“插入”标签时,甚至会遇到相同的错误。

任何人都想解决这个问题吗?

为澄清其用法,我有一个触发器,该触发器使用正确的meta_keys捕获信息,并将其放入具有相应post_id的另一个表中。

Screenshot of Select * From upplysingar

触发:

drop trigger if exists Trg_after_insert;
delimiter $$
create trigger Trg_after_insert
AFTER INSERT ON upplysingar
FOR EACH ROW

begin

SET @info = 
(
    SELECT meta_value
    FROM upplysingar
    WHERE post_id = NEW.post_id && meta_key = 'nem_name'
);

IF @info IS NOT NULL THEN

    INSERT INTO nemendur(id, nafn, email, kennitala, post_id)
    VALUES
    ('', @info, '', '', NEW.post_id);
ELSE
    SET @info = 
    (SELECT meta_value
     FROM upplysingar
     WHERE post_id = NEW.post_id && meta_key = 'nem_email');

    IF @info IS NOT NULL THEN

        UPDATE nemendur
        SET email = @info
        WHERE post_id = NEW.post_id;

    ELSE

        SET @info = (SELECT meta_value
                 FROM upplysingar
                 WHERE post_id = NEW.post_id && meta_key = 'nem_kennitala');

        if @info IS NOT NULL THEN

            UPDATE nemendur
            SET kennitala = @info
            WHERE post_id = NEW.post_id;
        end if;
    end if;
end if;

end $$
delimiter $$

预先感谢, WeirdGuy

1 个答案:

答案 0 :(得分:0)

INSERT INTO upplysingar (post_id, meta_key, meta_value) 
VALUES 
('1', 'nem_nafn', 'Bergur')

ID将自动递增。