mysql - 查找数据库中的重复字段,并更新行中的字段

标签 mysql sql

好的,我有一个包含 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/

相关文章:

mysql - 如何在 MySql 中打乱一列

python - 如何在 Python 中使用 select now() 导致另一个查询?

php - mysqli_query 有问题,返回 NULL 但查询在 phpmyadmin 中有效

mysql - 使用 WS02 从多个 MySQL 数据库中读取相似的表

mysql - 每个表的 last_update 时间戳?

sql - 模数(%)适用于十进制数据类型,但不适用于 float 或实数。为什么?

php - 为什么 mysqli_fetch_fields() 给我的结果不准确?

c# - 将枚举存储到数据库的平滑方法

mysql - SQL 返回错误总数

php - MySql 为每个 id 查找多行