How to select a row with a single value?

This is what my table looks like:

╔══════╦═══════╗ β•‘ USER β•‘ COLOR β•‘ ╠══════╬═══════╣ β•‘ a β•‘ Red β•‘ β•‘ b β•‘ Blue β•‘ β•‘ c β•‘ Blue β•‘ β•‘ b β•‘ Red β•‘ β•‘ a β•‘ Red β•‘ β•‘ c β•‘ White β•‘ β•šβ•β•β•β•β•β•β•©β•β•β•β•β•β•β•β• 

I need strings that have only color = "red".
It should return "a" ("b" also contains the value "Blue").

How to set a choice?

+4
source share
2 answers

You can use:

 select * from yourtable t1 where color = 'red' and exists (select user from yourtable t2 where t1.user = t2.user group by user having count(distinct color) = 1) 

See SQL Fiddle with Demo

Or without a subquery that you can use:

 select * from yourtable group by user HAVING SUM(color = 'red') = COUNT(*) 

See SQL Fiddle with Demo

+3
source

CHECK his request

 Select * from tbl where color = 'RED' AND USER not in (Select USER from tbl where color <> 'RED'); 
+2
source

All Articles