Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- +----+-----------+-------------+------------+--------+
- | id | title | description | createdAt | status |
- +----+-----------+-------------+------------+--------+
- | 1 | Product A | Desc A | 2019-07-19 | 1 |
- | 2 | Product B | Desc B | 2019-07-04 | 1 |
- +----+-----------+-------------+------------+--------+
- +----+-----------+-------------+
- | id | productId | highlightId |
- +----+-----------+-------------+
- | 1 | 1 | 9 |
- | 2 | 1 | 10 |
- | 3 | 1 | 11 |
- | 4 | 2 | 9 |
- | 5 | 2 | 10 |
- | 6 | 2 | 11 |
- | 7 | 2 | 12 |
- +----+-----------+-------------+
- +----+-----------+------------+
- | id | productId | categoryId |
- +----+-----------+------------+
- | 1 | 1 | 5 |
- | 2 | 1 | 7 |
- | 3 | 2 | 5 |
- | 4 | 2 | 7 |
- | 5 | 2 | 9 |
- +----+-----------+------------+
- SELECT
- pc.categoryId,
- COUNT(pc.productId)
- FROM
- product_categories pc,
- (
- SELECT
- t1.productId AS s1
- FROM
- product_categories t1
- JOIN
- product_highlights t2
- ON
- t1.productId = t2.productId
- WHERE
- t1.categoryId IN(7, 5) AND t2.highlightId IN(9, 10, 11)
- GROUP BY
- t1.productId
- HAVING
- 2 * 3 = COUNT(
- DISTINCT t1.categoryId,
- t2.highlightId
- )
- ) productsIds
- WHERE
- pc.productId = productsIds.s1
- GROUP BY
- pc.categoryId
- +------------+---------------------+
- | categoryId | COUNT(pc.productId) |
- +------------+---------------------+
- | 5 | 2 |
- | 7 | 2 |
- | 9 | 1 |
- +------------+---------------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement