I have the following table in my database,Now I want to fetch all records which start from one product value to the next product value.I mean all records starting from one 'product' to the next product value. Updated:
The Item_Type will always have two values either product or rule.So we have to find all the records starting from product value to the last rule value.
id Item_Type Product_ID
1 product 512350
2 rule 541
3 rule 5431
4 rule 5421
5 rule 5431
6 rule 5241
7 product 5350
8 rule 5541
9 rule 5341
10 rule 5421
11 rule 5141
12 rule 54411
13 product 51250
14 rule 541
15 rule 541
16 rule 541
17 rule 541
18 rule 541
19 product 512350
20 rule 541
21 rule 541
Please help me how it can done in mysql query.
For example, if you want to start from product 5350, you could do something horrible like this:
SELECT *
FROM thetable
WHERE id > (SELECT id
FROM thetable
WHERE Product_ID = 5350)
AND id < (SELECT MIN(id)
FROM thetable
WHERE Item_Type = product
AND id > (SELECT id FROM thetable WHERE Product_ID = 5350))
There might be a simpler query, but for such a specific situation I don't think efficiency is a must. I have to say again, though, that a two-table approach would have been much cleaner and would have made much more sense, sparing the need for such a bad querying style.
This is the basic SQL select query :
SELECT *
FROM $tableName
ORDER BY $orderColumn
LIMIT $firstRowNo, $rowsCount
Where
Examples: Query that will load first 3 products:
SELECT *
FROM myTable
ORDER BY Product_ID
LIMIT 0, 3;
Examples: Query that will load first 5,6,7,8,9 and 10th product:
SELECT *
FROM myTable
ORDER BY Product_ID
LIMIT 5, 5;
When you're using limit always sort row by selected column, it will help you determine how fields are sorted and the order will be always the same, even if you change the DB engine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.