学习cs50中的sql:runtimeerror:near“if”:语法错误

xuo3flqw  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(278)

我正在尝试创建一个名为stock的表,用于跟踪某人在购买了一定数量的股票后拥有多少股票。这是迄今为止我尝试过的最复杂的sql代码,我想我写错了什么。有人能发现错误吗?

db.execute("IF EXISTS (SELECT * FROM stock WHERE symbol = :symbol AND userid = :userid) 
    UPDATE stock SET (amount = amount + 1) WHERE userid = :userid 
    ELSE INSERT INTO stock (userid, symbol, amount) 
    VALUES ((SELECT id FROM users WHERE id = :userid), 
        :symbol, :amount", userid = session["user_id"], symbol = symbol, amount = share_number)
ctehm74n

ctehm74n1#

之后缺少一个括号

INSERT INTO stock (userid, symbol, amount) 
VALUES ((SELECT id FROM users WHERE id = :userid), :symbol, :amount

而不是 (SELECT id FROM users WHERE id = :userid) 你可以简单地写

INSERT INTO stock (userid, symbol, amount) 
VALUES (:userid, :symbol, :amount)

相关问题