代码之家  ›  专栏  ›  技术社区  ›  John

在列中查找具有匹配值的记录

sql
  •  1
  • John  · 技术社区  · 7 年前

    我想返回在phoneBox DB中具有匹配PhoneboxRecordId的所有记录。

    SELECT * FROM phoneBox where phoneBoxRecordIDs MATCH
    

    将返回:

    Id phoneBoxRecordIDs colour
    4  492948            Blue
    9  492948            Brown
    27 492948            Pink
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   xDJR1875    7 年前

    您可以按count>1. 但这只会返回phoneboxrecordid和具有该id的记录的#

    SELECT Count(*) [Count] , phoneBoxRecordIDs FROM phoneBox Group By phoneBoxRecordIDs Having Count(*) > 1

        2
  •  0
  •   Gordon Linoff    7 年前

    如果你想要的行 phoneBoxRecordIDs 出现多次,则ANSI标准方法使用窗口函数:

    select pb.*
    from (select pb.*, count(*) over (partition by phoneBoxRecordIDs) as cnt
          from phoneBox
         ) pb
    where cnt > 1
    order by phoneBoxRecordIDs;
    

    您还可以通过仅在存在匹配记录时返回记录来实现这一点:

    select pb.*
    from phoneBox pb
    where exists (select 1
                  from phoneBox pb2
                  where pb2.phoneBoxRecordIDs = pb.phoneBoxRecordIDs and
                        pb2.id <> pb.id
                 );