在列

时间:2017-10-11 23:44:24

标签: sql

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

SELECT * FROM phoneBox where phoneBoxRecordIDs MATCH

将返回:

Id phoneBoxRecordIDs colour
4  492948            Blue
9  492948            Brown
27 492948            Pink

2 个答案:

答案 0 :(得分:1)

您可以按计数>的字段进行分组1, 但这只会返回phoneboxrecordid和带有该ID

的记录数

{{1}}

答案 1 :(得分:0)

如果您希望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
             );