mysql执⾏复杂update更新,关联表以及使⽤sum()等函数使⽤ mysql 就不太强,⽽且写的复杂SQL多是 select 查询语句,最怕复杂的 update 语句了。
所以在这⾥总结⼀下
1.  关联需要更新的表或者根据 需要更新表,来执⾏更新
有⼀张订单明细表,我需要 更新 明细表⾥⾯的 ⾦额和重量, 这个 更新的依据是 根据 订单明细表⾥⾯的  某些数据的 来进⾏ 算出来 重量或者⾦额 来进⾏更新的。
当然 这个订单明细表 需要根据 订单主表 来进⾏查询出来。
⾸先 第⼀步 我写出来的SQL ,看起来也不算复杂
UPDATE gwqmshop_process_order_item set total_weight=1*count * (
SELECT units.unitWeight from (
al_unt as unitWeight from gwqmshop_process_order_item as isrc
where src_order_item_id=isrc.id
) as units
),
total_amount=total_weight*price
where id in (
SELECT item.id from
(
SELECT it.id  from  gwqmshop_process_order_item as it
where
it.disabled=0
der_id in (
SELECT o.id from gwqmshop_process_order o  where o.disabled=0 and o.data_type=3 atetime>'2018-05-26 22:00:00'
)
) item
);
更新的时候 where 语句必须 这样的写法,
否则 mysql 就会报错,因为 mysql 是不能 通过 查询 ⾃⾝然后来进⾏ 更新的。
报错内容:
MYSQL之You can't specify target table for update in FROM clause
上⾯的刚才我写的SQL语句, 是没有报错, 可是 执⾏更新之后, 更新的结果都 是 被更新为 null 了。
我这⾥是 mysql 5.6 不知道是不是 mysql的bug , 即没有报错,⼜被更新为 null 了。。。
原因是 因为  where src_order_item_id=isrc.id      这个语句, 在 mysql 中 是 获取 不到
订单明细表 ⾥⾯的 src_order_item_id  这个字段内容的。
如果我们改为 :
where isrc.id=gwqmshop_process_order_item.src_order_item_id
mysql 就会报错了, 报错是因为 不到  gwqmshop_process_order_item 表。
其实不管是 update 语句,还是 select 语句,  字段 通过  查询其他表,但是⼜需要更加当前查询的表的内容取管理的话,都是会报错的。因为 最外层的表的内容传不去⾥⾯的 临时表的。
例⼦ :
SELECT  i.id ,i.src_order_item_al_al_unt * (
al_unt
from gwqmshop_process_order_item isrc where isrc.id =i.src_order_item_id
)
from gwqmshop_process_order_item i
where i.disabled=0
der_id in (
SELECT o.id from gwqmshop_process_order o  where o.disabled=0 and o.data_type=3 atetime>'2018-05-26 22:00:00'
) ;
上⾯的语句肯定是没有问题的,如果改动⼀下就有问题了
SELECT  i.id ,i.src_order_item_al_al_unt * (
SELECT units.unitWeight from
(al_unt  as unitWeight
from gwqmshop_process_order_item isrc where isrc.id =i.src_order_item_id
)  as units
)
from gwqmshop_process_order_item i
where i.disabled=0
der_id in (
SELECT o.id from gwqmshop_process_order o  where o.disabled=0 and o.data_type=3 atetime>'2018-05-26 22:00:00'
) ;
然后就会报错:
Unknown column 'i.src_order_item_id' in 'where clause'
那为什么更新的时候不能使⽤第⼀种的⽅式呢?
UPDATE gwqmshop_process_order_item set gwqmshop_process_al_weight=1*gwqmshop_process_unt * (
al_unt as unitWeight from gwqmshop_process_order_item as isrc
where isrc.id=gwqmshop_process_order_item.src_order_item_id
),
gwqmshop_process_al_amount=gwqmshop_process_al_weight*gwqmshop_process_order_item.price
where gwqmshop_process_order_item.id in (
SELECT item.id from
(
SELECT it.id  from  gwqmshop_process_order_item as it
where
it.disabled=0
der_id in (
SELECT o.id from gwqmshop_process_order o  where o.disabled=0 and o.data_type=3 atetime>'2018-05-26 22:00:00'
)
) item
);
上⾯的更新语句就会爆错,还是之前的错误,不能通过查询⾃⾝来更新
报错:
You can't specify target table 'gwqmshop_process_order_item' for update in FROM clause
上⾯的办法都不能成功,然后我也乱写了⼀通,都搞不定,,,都⾃⼰的错,写代码写出BUG了,必须执⾏更新语句,,,⾃⼰的坑,⾃⼰填。。。
⽹上乱看,然后 ⾃⼰乱写,乱调试:
最终解决update SQL语句:
UPDATE gwqmshop_process_order_item,gwqmshop_process_order_item units
set gwqmshop_process_al_weight=1*gwqmshop_process_unt * (al_unt),
gwqmshop_process_al_amount=gwqmshop_process_al_weight*gwqmshop_process_order_item.price
where gwqmshop_process_order_item.id in (
批量更新sql语句SELECT item.id from
(
SELECT it.id  from  gwqmshop_process_order_item as it
where
it.disabled=0
der_id in (
SELECT o.id from gwqmshop_process_order o  where o.disabled=0 and o.data_type=3 atetime>'2018-05-26 22:00:00'
)
) item
) and units.id = gwqmshop_process_order_item.src_order_item_id;
原来只需要  update 的是, 再 来⼀个⾃⾝表 即可。
因为之前 没有这样想是因为 感觉可能是 会 报错,因为查询了⾃⾝表⼜更新了。。。
- You can't specify target table 'gwqmshop_process_order_item' for update in FROM clause
因为没有报错,可以成功是因为 mysql 的 底层区分开了吧。
gwqmshop_process_order_item,gwqmshop_process_order_item units 可能就是两个 不会产⽣关联的表吧。
2.  update 执⾏,使⽤SUM() 函数
订单明细表,已经更新了 重量和⾦额了,那么 还需要 更新 订单主表啊,因为订单主表也有 总的 重量和⾦额。。。
看起来挺简单,更加明细表求和,更加即可。
估计⼤家⼀般会这样写:
## 再更新订单的总理论重量和总理论价格
update gwqmshop_process_order
set total_weight =(
sum(
al_weight  from  gwqmshop_process_order_item as it1
der_id=id and it1.disabled=0
)
),
set total_price=(
sum(
al_amount  from  gwqmshop_process_order_item as it2
der_id=id and it2.disabled=0
)
)
where id in
(
SELECT mo.id from (
SELECT o.id as id from gwqmshop_process_order o  where o.disabled=0 and o.data_type=3 atetime>'2018-05-26 22:00:00'
) as mo
);
但是 执⾏之后 报错:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'al_weight  from  gwqmshop_process_order_item as it1
d' at line 4
语法错误,什么⿁,,,没有错误啊。反正就是不⾏。
看不问题,我试着简化⼀下SQL:
update gwqmshop_process_order ,
gwqmshop_process_order_item  it1
set gwqmshop_al_weight =al_weight)
where gwqmshop_process_order.id in
(
SELECT mo.id from (
SELECT o.id as id from gwqmshop_process_order o  where o.disabled=0 and o.data_type=3 atetime>'2018-05-26 22:00:00'
) as mo
)
der_id=gwqmshop_process_order.id and it1.disabled=0
;
就爆出来 :
Invalid use of group function
看起来 感觉是 使⽤不了 sum 函数啊。。。
想想也是,  sum 函数也算分组函数吧,分组求和的。。。
我们⼀般使⽤ sum 函数都是 在 select 语句中的,  没有这样直接使⽤的。。。
所以要改⼀下, 随便试了⼀下,没成功, 去百度看看
UPDATE Table_NAme SET PAR= summedValue
FROM  TAble_NAME t
JOIN (
SELECT ID, SUM(S_val) as summedvalue
FROM TABLE_NAME GROUP BY ID
) s on t.ID = s.ID
这样⼦ 感觉 好像⾏的通, 可是怎么使⽤MYSQL 写出来?
试着⽤join 关联:
update gwqmshop_process_order ,
inner join
(
SELECT gwqmshop_process_der_id as order_id ,sum(gwqmshop_process_al_weight) as sumweight  from gwqmshop_process_orde where gwqmshop_process_order_item.disabled=0
) as itsum der_id=gwqmshop_process_order.id
set gwqmshop_al_weight =itsum.sumweight
where gwqmshop_process_order.id in
(
SELECT mo.id from (
SELECT o.id as id from gwqmshop_process_order o  where o.disabled=0 and o.data_type=3 atetime>'2018-05-26 22:00:00'
) as mo
)
;
报错。。。
check the manual that corresponds to your MySQL server version for the right syntax to use near 'inner join
(
SELECT gwqmshop_process_der_id as order_id ,sum(gwq' at line 2
试试下⾯的:
update gwqmshop_process_order ,
(
SELECT gwqmshop_process_der_id as order_id ,sum(gwqmshop_process_al_weight) as sumweight  from gwqmshop_process_orde  GROUP BY  gwqmshop_process_der_id
) as itsum
left  JOIN itsum itsum1 der_id=gwqmshop_process_order.id
set gwqmshop_al_weight =itsum1.sumweight
where gwqmshop_process_order.id in
(
SELECT mo.id from (
SELECT o.id as id from gwqmshop_process_order o  where o.disabled=0 and o.data_type=3 atetime>'2018-05-26 22:00:00'
) as mo
)
;
Table 'yxshop.itsum' doesn't exist  报错,还是不⾏。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。