Skip to content

Instantly share code, notes, and snippets.

@RealNobody
Last active December 30, 2015 10:19
Show Gist options
  • Save RealNobody/7815078 to your computer and use it in GitHub Desktop.
Save RealNobody/7815078 to your computer and use it in GitHub Desktop.
SELECT
merchant_locations.address_id
, merchant_locations.merchant_id
, deals.*
, merchants.*
, addresses.*
FROM
merchant_locations
INNER JOIN
(
SELECT
merchant_locations.address_id AS address_id
FROM
merchant_locations
GROUP BY
merchant_locations.address_id
HAVING
COUNT(DISTINCT merchant_locations.merchant_id) > 1
) AS bad_addresss
ON (bad_addresss.address_id = merchant_locations.address_id)
INNER JOIN deals
ON (deals.merchant_id = merchant_locations.merchant_id)
INNER JOIN addresses
ON (addresses.id = merchant_locations.address_id)
INNER JOIN merchants
ON (merchants.id = merchant_locations.merchant_id)
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment