Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- +------------+--------------+--------------+----------------+
- | product_id | product_name | product_type | product_status |
- +------------+--------------+--------------+----------------+
- | 1 | apple | 1 | 1 |
- +------------+--------------+--------------+----------------+
- | 2 | orange | 1 | 1 |
- +------------+--------------+--------------+----------------+
- | 3 | car | 2 | 1 |
- +------------+--------------+--------------+----------------+
- | 4 | dress | 3 | 0 |
- +------------+--------------+--------------+----------------+
- SELECT * FROM products WHERE product_status = '1' AND product_type IN ('1', '2') GROUP BY product_type LIMIT 2
- +------------+--------------+
- | product_id | total_orders |
- +------------+--------------+
- | 1 | 20 |
- +------------+--------------+
- | 3 | 10 |
- +------------+--------------+
- | 4 | 10 |
- +------------+--------------+
- SELECT * FROM orders WHERE product_id = 'x' ORDER BY total_orders ASC
- SELECT p.*, o.total_orders FROM products p
- LEFT JOIN orders o ON p.product_id = o.product_id
- WHERE p.product_status = '1' AND p.product_type IN ('1', '2')
- 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