【问题标题】:Selecting one row from duplicate rows mysql/sqlite从重复行中选择一行mysql / sqlite
【发布时间】:2015-09-07 12:47:12
【问题描述】:

我的表(Data_A)由列组成:

_id | mac_address | bit 

有多个具有相同mac_address 的行,位可能为 0/1。
我想构建一个 sqlite 查询,我想在其中选择所有行的 _id,以便每行都有其唯一的mac_address,并且应该优先选择具有bit = 1 的行。

【问题讨论】:

    标签: sql database sqlite unique


    【解决方案1】:

    我认为以下在 SQLite 中有效:

    select a.*
    from data_a a
    where a._id = (select aa._id
                   from data_a aa
                   where aa.mac_address = a.mac_address
                   order by aa.bit desc
                   limit 1
                  );
    

    【讨论】:

      【解决方案2】:

      在 SQLite 3.7.11 或更高版本中,您可以简单地使用 min()/max() 从组中选择整行:

      SELECT _id, mac_address, max(bit)
      FROM Data_A
      GROUP BY mac_address
      

      【讨论】:

        猜你喜欢
        • 2010-11-26
        • 1970-01-01
        • 1970-01-01
        • 2012-02-05
        • 1970-01-01
        • 1970-01-01
        • 2018-08-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多