根据单个记录值选择一组记录

时间:2018-04-10 13:43:23

标签: mysql database

让我们考虑一下我有一张表如下

enter image description here

我需要选择具有相同“group_id”的记录,其中至少任何记录的“类型”等于1。

预期结果集必须与

类似

enter image description here

1 个答案:

答案 0 :(得分:0)

这应该有效:

select * from table where group_id in
(
    select group_id from table where type = 1
)

您也可以尝试使用join代替in

select a.* from table a
join 
(
   select group_id from table where type = 1
) b on b.group_id = a.group_id