sql - Limiting MySQL results based on a field -
i've table called products
in type
field type
value might 1 of these values 1,2,3,4
.
now i'would result
1. grouping results based on 'type' 2. , limit results each grouping 5.
how can accomplish this, i'm using next query
select *, ( ( case when product_title '%xyz%' 2 else 0 end ) + ( case when product_description '%xyz%' 1 else 0 end ) ) relevance products ( ( product_title '%xyz%' or product_description '%xyz%' ) ) , product_available = 1 , product_deleted <> 1 order relevance desc
select * products; +------------+------+ | product_id | type | +------------+------+ | 1 | 4 | | 2 | 3 | | 3 | 2 | | 4 | 4 | | 5 | 3 | | 6 | 1 | | 7 | 4 | | 8 | 3 | | 9 | 4 | | 10 | 2 | | 11 | 2 | | 12 | 1 | | 13 | 3 | | 14 | 2 | | 15 | 3 | | 16 | 1 | | 17 | 2 | | 18 | 1 | | 19 | 2 | | 20 | 4 | | 21 | 2 | +------------+------+ select x.* products x bring together products y on y.type = x.type , y.product_id <= x.product_id grouping x.product_id having count(*) <=3 order type , product_id; +------------+------+ | product_id | type | +------------+------+ | 6 | 1 | | 12 | 1 | | 16 | 1 | | 3 | 2 | | 10 | 2 | | 11 | 2 | | 2 | 3 | | 5 | 3 | | 8 | 3 | | 1 | 4 | | 4 | 4 | | 7 | 4 | +------------+------+
mysql sql
No comments:
Post a Comment