Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. +----+-----------+-------------+------------+--------+
  2. | id | title | description | createdAt | status |
  3. +----+-----------+-------------+------------+--------+
  4. | 1 | Product A | Desc A | 2019-07-19 | 1 |
  5. | 2 | Product B | Desc B | 2019-07-04 | 1 |
  6. +----+-----------+-------------+------------+--------+
  7.  
  8. +----+-----------+-------------+
  9. | id | productId | highlightId |
  10. +----+-----------+-------------+
  11. | 1 | 1 | 9 |
  12. | 2 | 1 | 10 |
  13. | 3 | 1 | 11 |
  14. | 4 | 2 | 9 |
  15. | 5 | 2 | 10 |
  16. | 6 | 2 | 11 |
  17. | 7 | 2 | 12 |
  18. +----+-----------+-------------+
  19.  
  20. +----+-----------+------------+
  21. | id | productId | categoryId |
  22. +----+-----------+------------+
  23. | 1 | 1 | 5 |
  24. | 2 | 1 | 7 |
  25. | 3 | 2 | 5 |
  26. | 4 | 2 | 7 |
  27. | 5 | 2 | 9 |
  28. +----+-----------+------------+
  29.  
  30. SELECT
  31. pc.categoryId,
  32. COUNT(pc.productId)
  33. FROM
  34. product_categories pc,
  35. (
  36. SELECT
  37. t1.productId AS s1
  38. FROM
  39. product_categories t1
  40. JOIN
  41. product_highlights t2
  42. ON
  43. t1.productId = t2.productId
  44. WHERE
  45. t1.categoryId IN(7, 5) AND t2.highlightId IN(9, 10, 11)
  46. GROUP BY
  47. t1.productId
  48. HAVING
  49. 2 * 3 = COUNT(
  50. DISTINCT t1.categoryId,
  51. t2.highlightId
  52. )
  53. ) productsIds
  54. WHERE
  55. pc.productId = productsIds.s1
  56. GROUP BY
  57. pc.categoryId
  58.  
  59. +------------+---------------------+
  60. | categoryId | COUNT(pc.productId) |
  61. +------------+---------------------+
  62. | 5 | 2 |
  63. | 7 | 2 |
  64. | 9 | 1 |
  65. +------------+---------------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement