【问题标题】:check duplicate mysql row检查重复的mysql行
【发布时间】:2011-06-02 16:06:53
【问题描述】:

我有一个 mysql 表,其中包含 field_onefield_twofield_three

我想检查 field_one 是否包含重复值(比如它是否唯一)。

如何在mysql中做到这一点?

谢谢

【问题讨论】:

    标签: mysql unique


    【解决方案1】:

    这将显示多次出现的field_one 的值:

    select field_one, count(*) as Count
    from MyTable
    group by field_one
    having count(*) > 1
    

    【讨论】:

    • 这在大表中不太可行。 OP 必须搜索值 >1 的东西。不过公平地说,OP 是相当模糊的。
    • 然后我应该使用什么?回声 mysql_result($q,0); ?
    • 我喜欢它。 value field_one 无论如何都应该是唯一的?但您也可以选择 id。你只会得到许多 id 中的一个,但它是一个开始
    • @Matthew 0.365sec 用于 650k 行表(在良好的服务器上运行)
    【解决方案2】:
    select field_one from tblName where field_one like %'@field_one'%
    

    select field_one from tblName where field_one = @filed_one
    

    【讨论】:

    • 然后我应该使用什么?回声 mysql_result($q,0); ?
    【解决方案3】:

    如果您只需要检查一列中的所有值是否唯一,您可以使用它来计算所有唯一值:

    select Count(Distinct column) from table
    

    ...并将其与列中所有值的数量进行比较:

    select Count(column) from table
    

    这可能会在大型表上消耗大量内存。

    【讨论】:

      猜你喜欢
      • 2018-03-11
      • 2014-03-15
      • 2013-10-04
      • 2017-01-12
      • 2012-05-09
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多