mysql更新错误

hhxx
  • 569

求教

function insert2db()
{
    TABLE=$1
    TIME=$2
    COUNT=$3
    TYPE=$4
    SER=$5
    mysql -h10.134.72.72 -uroot << EOF
    use GalaxyConfig;
    create table IF NOT EXISTS \`$TABLE\` (
        id INT(11) NOT NULL AUTO_INCREMENT,
        time DATETIME UNIQUE,
        count BIGINT,
        type VARCHAR(10),
        service VARCHAR(10),
        primary key (id)
    );
    IF EXISTS(select * from \`$TABLE\` WHERE time='$TIME' AND type='$TYPE' AND service='$SER') THEN
        {update \`$TABLE\` SET count=count+$COUNT WHERE time='$TIME' AND type='$TYPE' AND service='$SER'}
    ELSE 
        {insert into \`$TABLE\`(time, count, type, service) VALUES('$TIME', $COUNT, '$TYPE', '$SERVICE')}
    END IF;
EOF
}

上面的mysql代码哪里有问题

回复
阅读 1.6k
2 个回答

报什么错误啊?可以使用insert into xxx on duplicated key update xxxx 一条语句搞定的哦。

不能哦,我要根据三个键相同才行更新呀,不是一个主键,报的错就说语法错误啊,我也莫名其妙觉得

宣传栏