code_junkie

Many to many table queries

Nov 11th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. eid | fid
  2. ----+----
  3. 1 | A
  4. 1 | B
  5. 1 | C
  6. 2 | B
  7. 2 | C
  8. 3 | A
  9. 3 | C
  10. 4 | A
  11. 4 | B
  12. 5 | B
  13.  
  14. select t1.eid
  15. from t t1
  16. where t1.fid = 'B'
  17. and not exists
  18. (select 1
  19. from t t2
  20. where t2.eid = t1.eid
  21. and t2.fid = 'A')
  22.  
  23. Select eid from t where fid = 'B'
  24. EXCEPT
  25. select eid from t where fid = 'A'
  26.  
  27. SELECT DISTINCT t1.eid
  28. FROM table1 AS t1
  29. LEFT JOIN table1 AS t2 ON (t1.eid = t2.eid AND t2.fid = 'A')
  30. WHERE t2.eid IS NULL
  31. AND t1.fid = 'B';
  32.  
  33. SELECT DISTINCT t1.eid
  34. FROM table1 AS t1
  35. LEFT JOIN table1 AS t2 ON (t1.eid = t2.eid AND t2.fid = 'A')
  36. WHERE t2.eid IS NULL
  37. AND t1.fid = 'B';
  38.  
  39. select eid
  40. from t
  41. where fid = 'A'
  42. minus
  43. select eid
  44. from t
  45. where fid = 'B'
Add Comment
Please, Sign In to add comment