rlfprog

Three-User Conversation Query

Jan 5th, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.84 KB | None | 0 0
  1. -- Determine if a conversation exists between users 32, 3, and 4.
  2. SELECT cu1.id
  3. FROM   conversation_user cu1
  4. WHERE  cu1.user_id = 32
  5. AND    EXISTS (
  6.    SELECT 1
  7.    FROM   conversation_user cu2
  8.    WHERE  cu2.conversation_id = cu1.conversation_id
  9.    AND    cu2.user_id = 3
  10.    )
  11. AND    EXISTS (
  12.    SELECT 1
  13.    FROM   conversation_user cu3
  14.    WHERE  cu3.conversation_id = cu1.conversation_id
  15.    AND    cu3.user_id = 4
  16.    )
  17. AND NOT EXISTS (
  18.    SELECT 1
  19.    FROM   conversation_user cu4
  20.    WHERE  cu4.conversation_id = cu1.conversation_id
  21.    AND    cu4.user_id NOT IN (3,4,32)
  22.    );
  23.  
  24.  
  25. TABLE "public.auth_user"
  26.     COLUMN    |           TYPE           |                       Modifiers                        
  27. --------------+--------------------------+--------------------------------------------------------
  28.  id           | INTEGER                  | NOT NULL DEFAULT NEXTVAL('auth_user_id_seq'::regclass)
  29.  password     | CHARACTER VARYING(128)   | NOT NULL
  30.  username     | CHARACTER VARYING(30)    | NOT NULL
  31. Indexes:
  32.     "auth_user_pkey" PRIMARY KEY, btree (id)
  33.     "auth_user_username_key" UNIQUE CONSTRAINT, btree (username)
  34.     "auth_user_username_like" btree (username varchar_pattern_ops)
  35. Referenced BY:
  36.     TABLE "conversation_user" CONSTRAINT "conversation_user_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED
  37.     TABLE "message" CONSTRAINT "message_from_user_id_fkey" FOREIGN KEY (from_user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED
  38.  
  39.  
  40. TABLE "public.conversation"
  41.    COLUMN   |           TYPE           |                         Modifiers                        
  42. ------------+--------------------------+-----------------------------------------------------------
  43.  id         | INTEGER                  | NOT NULL DEFAULT NEXTVAL('conversation_id_seq'::regclass)
  44.  start_date | TIMESTAMP WITH TIME zone | NOT NULL
  45. Indexes:
  46.     "conversation_pkey" PRIMARY KEY, btree (id)
  47. Referenced BY:
  48.     TABLE "conversation_user" CONSTRAINT "conversation_id_refs_id_4344ca71" FOREIGN KEY (conversation_id) REFERENCES conversation(id) DEFERRABLE INITIALLY DEFERRED
  49.     TABLE "message" CONSTRAINT "message_conversation_id_fkey" FOREIGN KEY (conversation_id) REFERENCES conversation(id) DEFERRABLE INITIALLY DEFERRED
  50.  
  51.  
  52. TABLE "public.conversation_user"
  53.      COLUMN      |  TYPE   |                           Modifiers                            
  54. -----------------+---------+----------------------------------------------------------------
  55.  id              | INTEGER | NOT NULL DEFAULT NEXTVAL('conversation_user_id_seq'::regclass)
  56.  conversation_id | INTEGER | NOT NULL
  57.  user_id         | INTEGER | NOT NULL
  58. Indexes:
  59.     "conversation_user_pkey" PRIMARY KEY, btree (id)
  60.     "conversation_user_conversation_id_user_id_key" UNIQUE CONSTRAINT, btree (conversation_id, user_id)
  61.     "conversation_user_conversation_id" btree (conversation_id)
  62.     "conversation_user_user_id" btree (user_id)
  63. Foreign-KEY constraints:
  64.     "conversation_id_refs_id_4344ca71" FOREIGN KEY (conversation_id) REFERENCES conversation(id) DEFERRABLE INITIALLY DEFERRED
  65.     "conversation_user_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED
  66.  
  67.  
  68. auth_user TABLE DATA:
  69.  id |   username    
  70. ----+---------------
  71.   1 | user1
  72.   3 | user3
  73.   4 | user4
  74.  32 | me
  75. (4 ROWS)
  76.  
  77.  
  78. conversation TABLE DATA:
  79.  id |          start_date          
  80. ----+-------------------------------
  81.   1 | 2015-01-05 12:14:24.098392-08
  82.   2 | 2015-01-05 12:14:24.124386-08
  83.   3 | 2015-01-05 12:14:24.143839-08
  84. (3 ROWS)
  85.  
  86.  
  87.  id | conversation_id | user_id
  88. ----+-----------------+---------
  89.   1 |               1 |      32
  90.   2 |               1 |       3
  91.   3 |               2 |      32
  92.   4 |               2 |       3
  93.   5 |               2 |       4
  94.   6 |               3 |       3
  95.   7 |               3 |       1
  96. (7 ROWS)
Advertisement
Add Comment
Please, Sign In to add comment