Skip to content

Instantly share code, notes, and snippets.

@omarqureshi
Forked from nalindz/gist:1774443
Created February 8, 2012 23:34
Show Gist options
  • Save omarqureshi/1775436 to your computer and use it in GitHub Desktop.
Save omarqureshi/1775436 to your computer and use it in GitHub Desktop.
This is the query:
SELECT * FROM questions
LEFT JOIN messages on questions.id = messages.question_id
LEFT JOIN questions_skip_counts qsc on questions.id = qsc.question_id AND questions.user_id = qsc.user_id
WHERE messages.sender_id = NULL and questions.user_id != 'FOO' AND qsc.skip_count =
(SELECT min(qsc.skip_count) FROM questions LEFT
JOIN messages on questions.id = messages.question_id LEFT JOIN questions_skip_counts qsc on questions.id = qsc.question_id AND questions.user_id = qsc.user_id WHERE messages.sender_id = NULL and questions.user_id != 'FOO')
And the schema:
create_table "questions", :force => true do |t|
t.integer "user_id"
t.string "image"
t.string "question_type", :default => "thumbs"
t.integer "topic_id"
t.integer "location_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "text"
t.integer "num_thumbs_up", :default => 0
t.integer "num_thumbs_down", :default => 0
t.integer "messages_count", :default => 0
t.integer "read_messages_count", :default => 0
t.integer "last_answer_id"
end
create_table "messages", :force => true do |t|
t.integer "sender_id"
t.integer "receiver_id"
t.string "text"
t.integer "location_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "question_id"
t.string "vote", :default => "none"
t.boolean "read", :default => false
end
create_table "question_skip_counts", :force => true do |t|
t.integer "user_id"
t.integer "question_id"
t.integer "skip_count", :default => 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment