简体   繁体   中英

Selecting a Group of records based on a single Record Value

Let us consider we I have a table as follows

在此处输入图片说明

I need to select the records with same "group_id" s where at least anyone record's "type" is equal to 1.

The expected result set must be like

在此处输入图片说明

This should work:

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

You can also try join instead of in

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM