I’m working with Rails and I have a Link ActiveRecord model with this table:
create_table "links", :force => true do |t|
t.string "name", :null => false
t.integer "sender_id", :null => false
t.integer "receiver_id", :null => false
end
add_index "links", ["name"], :name => "index_links_on_name"
add_index "links", ["sender_id", "receiver_id"], :name => "index_links_on_sender_id_and_receiver_id", :unique => true
What I would like to do is to SELECT every links WHERE name = "follow" which exist from the source to the destination AND from the destination to the source.
For instance, having the following entries:
id | name | sender_id | receiver_id
---------------------------------------
66 | "follow" | 1 | 2
67 | "follow" | 2 | 1
68 | "follow" | 1 | 3
69 | "fuu" | 3 | 1
70 | "bar" | 2 | 25
71 | "bar" | 25 | 2
…I would like to just get 66 and 67 ids as a result. Because sender_id 1 and receiver_id 67 are linked from source to destination and also from destination to source (with a “follow” name in both cases).
Can we do it, with just one SQL query (in SQL directly or ActiveRecord ORM)? Many thanks!
Try Following Query>>