Advertisement
cdsatrian

Implementing Limit Per Category

Apr 21st, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 4.45 KB | None | 0 0
  1. -- data category
  2.  
  3. DROP TABLE IF EXISTS my_category;
  4. CREATE TABLE IF NOT EXISTS my_category(
  5.   id INT AUTO_INCREMENT PRIMARY KEY,
  6.   name VARCHAR(25)
  7. ) ENGINE=MYISAM;
  8.  
  9. INSERT INTO my_category(name)
  10. VALUES
  11. ('makanan'),
  12. ('minuman'),
  13. ('pakaian');
  14.  
  15. SELECT * FROM my_category;
  16.  
  17. +----+---------+
  18. | id | name    |
  19. +----+---------+
  20. |  1 | makanan |
  21. |  2 | minuman |
  22. |  3 | pakaian |
  23. +----+---------+
  24. 3 rows in set (0.00 sec)
  25.  
  26. -- data item
  27.  
  28. DROP TABLE IF EXISTS my_item;
  29. CREATE TABLE IF NOT EXISTS my_item(
  30.   id INT AUTO_INCREMENT PRIMARY KEY,
  31.   name VARCHAR(25),
  32.   category INT
  33. ) ENGINE=MYISAM;
  34.  
  35. INSERT INTO my_item(name,category)
  36. VALUES
  37. ('nasi goreng',1),
  38. ('bubur ayam',1),
  39. ('sate ayam',1),
  40. ('pecel lele',1),
  41. ('empek-empek',1),
  42. ('martabak manis',1),
  43. ('mie ayam',1),
  44. ('es teh manis',2),
  45. ('coffee latte',2),
  46. ('soda gembira',2),
  47. ('kopi susu',2),
  48. ('orange juice',2),
  49. ('lemonade',2),
  50. ('t-shirt',3),
  51. ('kemeja',3),
  52. ('tuxedo',3),
  53. ('blus',3),
  54. ('batik',3);
  55.  
  56. SELECT * FROM my_item;
  57.  
  58. +----+----------------+----------+
  59. | id | name           | category |
  60. +----+----------------+----------+
  61. |  1 | nasi goreng    |        1 |
  62. |  2 | bubur ayam     |        1 |
  63. |  3 | sate ayam      |        1 |
  64. |  4 | pecel lele     |        1 |
  65. |  5 | empek-empek    |        1 |
  66. |  6 | martabak manis |        1 |
  67. |  7 | mie ayam       |        1 |
  68. |  8 | es teh manis   |        2 |
  69. |  9 | coffee latte   |        2 |
  70. | 10 | soda gembira   |        2 |
  71. | 11 | kopi susu      |        2 |
  72. | 12 | orange juice   |        2 |
  73. | 13 | lemonade       |        2 |
  74. | 14 | t-shirt        |        3 |
  75. | 15 | kemeja         |        3 |
  76. | 16 | tuxedo         |        3 |
  77. | 17 | blus           |        3 |
  78. | 18 | batik          |        3 |
  79. +----+----------------+----------+
  80. 18 rows in set (0.00 sec)
  81.  
  82. -- Limiting Item per Category
  83. SELECT *
  84. FROM
  85. (
  86.   SELECT
  87.     my_item.id,
  88.     my_item.name,
  89.     @c:=IF(@cat=my_item.category,@c+1,1) AS c,
  90.     @cat:=my_item.category AS cat,
  91.     my_category.name AS category
  92.   FROM
  93.     my_item
  94.     JOIN my_category ON my_item.category=my_category.id,
  95.     (
  96.       SELECT
  97.         @c:=0,
  98.         @cat:=NULL
  99.     ) AS a
  100.   ORDER BY
  101.     my_item.category
  102. ) AS b
  103. WHERE c<=4
  104. ORDER BY cat,c;
  105.  
  106. +----+--------------+----------+------+------+
  107. | id | name         | category | c    | cat  |
  108. +----+--------------+----------+------+------+
  109. |  1 | nasi goreng  | makanan  |    1 |    1 |
  110. |  2 | bubur ayam   | makanan  |    2 |    1 |
  111. |  3 | sate ayam    | makanan  |    3 |    1 |
  112. |  4 | pecel lele   | makanan  |    4 |    1 |
  113. |  8 | es teh manis | minuman  |    1 |    2 |
  114. |  9 | coffee latte | minuman  |    2 |    2 |
  115. | 10 | soda gembira | minuman  |    3 |    2 |
  116. | 11 | kopi susu    | minuman  |    4 |    2 |
  117. | 14 | t-shirt      | pakaian  |    1 |    3 |
  118. | 15 | kemeja       | pakaian  |    2 |    3 |
  119. | 16 | tuxedo       | pakaian  |    3 |    3 |
  120. | 17 | blus         | pakaian  |    4 |    3 |
  121. +----+--------------+----------+------+------+
  122. 12 rows in set (0.01 sec)
  123.  
  124. -- Limiting Random Item per Category
  125. SELECT *
  126. FROM
  127. (
  128.   SELECT
  129.     item.id,
  130.     item.name,
  131.     @c:=IF(@cat=item.category,@c+1,1) AS c,
  132.     @cat:=item.category AS cat,
  133.     item.category
  134.   FROM
  135.     (
  136.       SELECT
  137.         i.id,
  138.         i.name,
  139.         my_category.name AS category
  140.       FROM
  141.         (SELECT * FROM my_item ORDER BY RAND()) AS i
  142.         JOIN my_category ON i.category=my_category.id
  143.       ORDER BY
  144.         i.category
  145.     ) AS item,
  146.     (
  147.       SELECT
  148.         @c:=0,
  149.         @cat:=NULL
  150.     ) AS a
  151.   ORDER BY
  152.     item.category
  153.  ) AS b
  154. WHERE c<=4
  155. ORDER BY cat,c;  
  156.  
  157. +----+----------------+------+---------+----------+
  158. | id | name           | c    | cat     | category |
  159. +----+----------------+------+---------+----------+
  160. |  2 | bubur ayam     |    1 | makanan | makanan  |
  161. |  6 | martabak manis |    2 | makanan | makanan  |
  162. |  7 | mie ayam       |    3 | makanan | makanan  |
  163. |  1 | nasi goreng    |    4 | makanan | makanan  |
  164. |  9 | coffee latte   |    1 | minuman | minuman  |
  165. | 11 | kopi susu      |    2 | minuman | minuman  |
  166. | 12 | orange juice   |    3 | minuman | minuman  |
  167. |  8 | es teh manis   |    4 | minuman | minuman  |
  168. | 18 | batik          |    1 | pakaian | pakaian  |
  169. | 14 | t-shirt        |    2 | pakaian | pakaian  |
  170. | 15 | kemeja         |    3 | pakaian | pakaian  |
  171. | 17 | blus           |    4 | pakaian | pakaian  |
  172. +----+----------------+------+---------+----------+
  173. 12 rows in set (0.00 sec)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement