我已经写了左连接查询,我希望左表的product_code列应该由右表的product_code列按以下查询条件更新
update stock_details_mt as s left join
(select min(product_code),product_name,mrp,sales_rate from stock_details_mt group by product_name,mrp,sales_rate having count(*)>1)as s2 set
s.product_code=s2.product_code on s.product_name=s2.product_name where s.mrp=s2.mrp and s.sales_rate=s2.sales_rate ;
答案 0 :(得分:0)
你似乎已经失去了连接和集之间的情节,我会怀疑左连接。试试这个。
update stock_details_mt as s join
(select min(product_code) product_code,product_name,mrp,sales_rate
from stock_details_mt
group by product_name,mrp,sales_rate
having count(*)>1) as s2
on s.product_name=s2.product_name and s.mrp=s2.mrp and s.sales_rate=s2.sales_rate
set s.product_code=s2.product_code
where 1 = 1;
所以给出
drop table if exists stock_details_mt;
create table stock_details_mt
(product_code int, product_name varchar(10), mrp int, sales_rate int);
insert into stock_details_mt values
(2,'aaa',1,1),(1,'aaa',1,1),(10,'aaa',1,1),
(10,'bbb',1,1);
结果
+--------------+--------------+------+------------+
| product_code | product_name | mrp | sales_rate |
+--------------+--------------+------+------------+
| 1 | aaa | 1 | 1 |
| 1 | aaa | 1 | 1 |
| 1 | aaa | 1 | 1 |
| 10 | bbb | 1 | 1 |
+--------------+--------------+------+------------+
4 rows in set (0.00 sec)