[英]Compare row with another row in same table
I have a table with the following structure: 我有一个具有以下结构的表:
id_language id_product description
1 10 AAAA
2 10 BBBB
3 10 AABB
1 11 CCCC
2 11 CCCC
3 11 CCAA
What I want to do is to find all products that have same description in both languages and replace the description of language 2 with an empty value. 我要做的是找到所有两种语言中具有相同描述的产品,并用一个空值替换语言2的描述。 So in the example above row 1 and 2 would be left untouched.
因此,在上面的示例中,第1行和第2行将保持不变。 However description of product 11 and language 2 would be set to "".
但是,产品11和语言2的描述将设置为“”。 Like this:
像这样:
id_language id_product description
1 10 AAAA
2 10 BBBB
3 10 AABB
1 11 CCCC
2 11
3 11 CCAA
Is it possible to do this with just SQL query? 是否可以仅使用SQL查询来执行此操作? I am using MySQL.
我正在使用MySQL。
Assuming you have only two languages per product, this seems to do what you want: 假设每种产品只有两种语言,这似乎可以满足您的要求:
update t join
(select id_product, max(id_language) as maxidl
from t
group by id_product
having min(description) = max(description)
) tt
on t.id_product = tt.id_product and t.id_language = tt.maxidl
set description = null;
Use this one For MySql 将此用于MySql
UPDATE Table SET description=''
WHERE (id_product,id_language) in(
SELECT id_product,MAX(id_language) FROM Table
GROUP BY description,id_product
HAVING COUNT(id_language)>1
)
This one works (see it working live here in a sqlfiddle): 这一个工程(见它的工作生活在这里的sqlfiddle):
UPDATE t
JOIN (
SELECT
t1.id_product,
MIN(t1.id_language) AS min_lang,
t1.description
FROM t t1
JOIN t t2 ON t1.id_product = t2.id_product
AND t1.description = t2.description
AND t1.id_language != t2.id_language
GROUP BY t1.id_product
) sq ON t.id_product = sq.id_product
AND t.description = sq.description
SET t.description = ''
WHERE t.id_language > sq.min_lang;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.