好的,我有一个包含 3000 行的表。每行都是唯一的,但有些字段是重复的。 我想用重复的字段更新行。我很难用语言表达这一点,所以如果我弄错了,我很抱歉。
For example, the table looks like this:
+----+--------+--------+-----------+-------+-------+------+--+
| id | name | place | day | time1 | time2 | dupe | |
+----+--------+--------+-----------+-------+-------+------+--+
| 1 | George | Garden | Sunday | 12:00 | 13:00 | 0 | |
| 2 | George | House | Monday | 15:00 | 18:00 | 0 | |
| 3 | David | School | Wednesday | 15:00 | 18:00 | 0 | |
| 4 | Stan | Church | Sunday | 12:00 | 13:00 | 0 | |
+----+--------+--------+-----------+-------+-------+------+--+
我想运行一个 mysql 查询来检查表中名称字段中的重复名称,如果重复,则将 dupe 字段标记为 1。
因此第 1 行和第 2 行中的 dupe 字段应为“1”,其余应为“0”。
感谢您提供的任何帮助!我希望我解释正确。
最佳答案
您可以在 CASE 语句中使用 EXISTS:
select
t.*,
case
when exists (
select 1 from tablename
where id <> t.id and name = t.name
) then 1
else 0
end dupe
from tablename t
关于mysql - 查找数据库中的重复字段,并更新行中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56401076/