UPDATE `adv` AS A SET `exchange` = ( CASE WHEN (SELECT MAX(B.`id`) FROM `adv` AS B WHERE B.`id` = A.`id` GROUP BY B.store_id ) = A.`id` THEN 1 ELSE 0 END );
执行的话会报错,语句 的目的是要设置相同的store_id内的最大id的exchange字段为1,其余为0, 问一下这样写为什么不行。
解决方案
20
mysql在开发方面限制比较多。
你试试这个,可能也会报错,你要修改一下sql mode:
update adv as a, ( select a.id,a.store_id,(select count(*) from adv b where a.store_id = b.store_id and a.id<=b.id) rn from adv a )b set a.exchange = case when b.rn = 1 then 1 else 0 end where a.id = b.id
20
UPDATE `adv` AS A left join (SELECT store_id, MAX(`id`) as id FROM `adv` GROUP BY store_id ) b on a.id=b.id set exchange=1-ISNULL(b.id)