Skip to content

Instantly share code, notes, and snippets.

@minikomi
Created August 15, 2011 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minikomi/1146043 to your computer and use it in GitHub Desktop.
Save minikomi/1146043 to your computer and use it in GitHub Desktop.
sql from sales => prefectures (unique)
request:
@sales = Sale.includes([{:shop => :prefecture},:brands])
.order('finish DESC').where("finish > ?", Date.today)
.joins(:shop => { :city => :prefecture })
.select('sales.*, DISTINCT prefecture.id')
"Sale Load (62.0ms) SELECT "sales"."id" AS t0_r0, "sales"."title" AS t0_r1, "sales"."description"
AS t0_r2, "sales"."invite" AS t0_r3, "sales"."shop_id" AS t0_r4, "sales"."start" AS t0_r5, "sales"."finish"
AS t0_r6, "sales"."created_at" AS t0_r7, "sales"."updated_at" AS t0_r8, "shops"."id" AS t1_r0, "shops"."name"
AS t1_r1, "shops"."kana" AS t1_r2, "shops"."address" AS t1_r3, "shops"."building" AS t1_r4, "shops"."openclose"
AS t1_r5, "shops"."holiday" AS t1_r6, "shops"."description" AS t1_r7, "shops"."tel" AS t1_r8, "shops"."email"
AS t1_r9, "shops"."url" AS t1_r10, "shops"."city_id" AS t1_r11, "prefectures"."id" AS t2_r0, "prefectures"."name"
AS t2_r1, "prefectures"."kana" AS t2_r2, "brands"."id" AS t3_r0, "brands"."name" AS t3_r1, "brands"."kana" AS t3_r2, "brands"."url"
AS t3_r3, "brands"."country_id" AS t3_r4 FROM "sales" INNER JOIN "shops" ON "shops"."id" = "sales"."shop_id"
INNER JOIN "cities" ON "cities"."id" = "shops"."city_id" INNER JOIN "prefectures" ON "prefectures"."id" = "cities"."prefecture_id"
LEFT OUTER JOIN "cities" "prefectures_shops_join" ON "shops"."city_id" = "prefectures_shops_join"."id"
LEFT OUTER JOIN "prefectures" "prefectures_shops" ON "prefectures_shops"."id" = "prefectures_shops_join"."prefecture_id"
LEFT OUTER JOIN "brands_sales" ON "brands_sales"."sale_id" = "sales"."id" LEFT OUTER JOIN "brands"
ON "brands"."id" = "brands_sales"."brand_id" WHERE (finish > '2011-08-15') ORDER BY finish DESC"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment