我正在尝试更新"%2017-08-01%"之间的金额和 "%2017年8月7日%"到下一个日期的金额"%2017-08-07%" e.auto =" true"在交易表中。但是,mysql正在返回
1064 - 您的SQL语法出错;查看与MariaDB服务器版本对应的手册,以获取正确的语法 靠近' FROM user as u JOIN e_wallet as e ON e.id = u.ewallet JOIN transacti'在第1行
update t set t.amount = (
select t.amount
FROM user as u
JOIN e_wallet as e ON e.id = u.ewallet
JOIN transaction as t on t.user = u.id
where e.auto="true"
and t.create_date like "%2017-08-07%"
)
FROM user as u
JOIN e_wallet as e ON e.id = u.ewallet
JOIN transaction as t on t.user = u.id
where e.auto="true"
and t.create_date BETWEEN "%2017-08-01%" and "%2017-08-07%"
答案 0 :(得分:1)
如果忽略子查询,则UPDATE
查询如下所示:
UPDATE t
SET t.amount = ...
FROM user AS u ...
JOIN e_wallet AS e ...
您在FROM
查询中不能拥有UPDATE
条款,而且导致语法错误的原因是什么。语法是:
UPDATE tablename AS t1
JOIN tablename2 AS t2 ON ...
JOIN tablename3 AS t3 ON ...
SET ...
WHERE ...
所以它应该是:
UPDATE transaction AS t
JOIN user AS u ON u.id = t.user
JOIN e_wallet AS e ON e.id = u.ewallet
SET t.amount = (
select t.amount
FROM user as u
JOIN e_wallet as e ON e.id = u.ewallet
JOIN transaction as t on t.user = u.id
where e.auto="true"
and t.create_date like "%2017-08-07%"
)
WHERE e.auto = "true"
AND t.create_date BETWEEN "2017-08-01" and "2017-08-07"
这应解决语法错误,但我不确定它会做你想要的。子查询与主查询不相关,因此它将返回符合条件的所有事务的数量。但是当您使用子查询作为表达式时,它必须最多返回一行。您可能只想第二次加入transaction
表。
UPDATE transaction AS t
JOIN user AS u ON u.id = t.user
JOIN e_wallet AS e ON e.id = u.ewallet
JOIN transaction AS t1 ON t1.user = t.user
SET t.amount = t1.amount
WHERE e.auto = "true"
AND t.create_date BETWEEN "2017-08-01" and "2017-08-07"
AND t1.create_date LIKE '%2017-08-07%'