Guest User

Untitled

a guest
Dec 17th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. +------------+--------------+--------------+----------------+
  2. | product_id | product_name | product_type | product_status |
  3. +------------+--------------+--------------+----------------+
  4. | 1 | apple | 1 | 1 |
  5. +------------+--------------+--------------+----------------+
  6. | 2 | orange | 1 | 1 |
  7. +------------+--------------+--------------+----------------+
  8. | 3 | car | 2 | 1 |
  9. +------------+--------------+--------------+----------------+
  10. | 4 | dress | 3 | 0 |
  11. +------------+--------------+--------------+----------------+
  12.  
  13. SELECT * FROM products WHERE product_status = '1' AND product_type IN ('1', '2') GROUP BY product_type LIMIT 2
  14.  
  15. +------------+--------------+
  16. | product_id | total_orders |
  17. +------------+--------------+
  18. | 1 | 20 |
  19. +------------+--------------+
  20. | 3 | 10 |
  21. +------------+--------------+
  22. | 4 | 10 |
  23. +------------+--------------+
  24.  
  25. SELECT * FROM orders WHERE product_id = 'x' ORDER BY total_orders ASC
  26.  
  27. SELECT p.*, o.total_orders FROM products p
  28. LEFT JOIN orders o ON p.product_id = o.product_id
  29. WHERE p.product_status = '1' AND p.product_type IN ('1', '2')
  30. GROUP BY p.product_type ORDER BY CASE WHEN o.total_orders IS NULL THEN 0 ELSE o.total_orders END ASC LIMIT 2
Add Comment
Please, Sign In to add comment