VPK
VPK

Reputation: 3090

Get unique values from a MySql table column grouped with other column

This might be a very basic query practice for MySql experts, but I can't get a result as I'm expecting.

Below is the MySql query and the output pane. What I want to achieve is to get the bug_ids which is not present in the records for all cell_ids. In other words, looking at the records below we can see bug_ids 1,2,3,4 are present for all cell_ids 32,33,34. I want the records other than these bug_ids. So the expected output would be something like this,

project_id cell_id bug_id test_set_id case_id  
106        32      16     1           5  
106        33      16     1           5  
106        34       7     1           5  

I have also tried using self join but couldn't found a proper query.

Query with the output

Please let me know if you need more info. Any help will be greatly appreciated. Thanks guys.

Upvotes: 0

Views: 503

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270001

You can get the list of bugs that are not in all the cells by doing:

select bug_id
from tran_cell_bug tcb
where cell_id in (32, 33, 34)
group by bug_id
having count(distinct cell_id) <> 3;

You can then use join to get the original values:

select tcb.*
from tran_cell_bug tcb join
     (select bug_id
      from tran_cell_bug tcb
      where cell_id in (32, 33, 34)
      group by bug_id
      having count(distinct cell_id) <> 3
     ) b
     on tcb.bug_id = b.bug_id;

Upvotes: 1

Related Questions