Skip to content

Instantly share code, notes, and snippets.

@zephiransas
Created May 28, 2012 06:27
Show Gist options
  • Save zephiransas/2817686 to your computer and use it in GitHub Desktop.
Save zephiransas/2817686 to your computer and use it in GitHub Desktop.
SELECT * FROM shops WHERE
(
SELECT management_division_value
FROM shop_management_divisions
WHERE management_division_id = 7 AND shop_management_divisions.shop_id = shops.id
) = 1 AND
(
SELECT management_division_value
FROM shop_management_divisions
WHERE management_division_id = 8 AND shop_management_divisions.shop_id = shops.id
) = 4
@kazuhisa
Copy link

a = ShopManagementDivision.where(:management_division_id => 7,:management_division_value => 1)
b = ShopManagementDivision.where(:management_division_id => 8,:management_division_value => 4)
shops = Shop.where(:id => a.map{|v| v.shop_id}).where(:id => b.map{|v| v.shop_id})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment