Is there a way to setup an start or stop element in mysql ? I want to to select every element starting with id = 20 and stops when level < 3 the first time.
A:
In mysql I think this is hard to do in one query, I thnk youd have to do something like
SELECT @ordernr = MIN(ordernr)
FROM ...
WHERE id = 20
AND level < 3
If you have the id=20 rows:
ordernr level
1 34
2 23
3 88
4 2 <- it will return 4
5 12
6 22
7 2
Then use the result:
SELECT ...
FROM ...
WHERE id = 20
AND ordernr <= @ordernr
It will return ordernr level 1 34 2 23 3 88 4 2
Brimstedt
2009-12-18 12:06:41
The first SELECT is going to suppress all levels > 3, which is probably not what the asker wanted!
Carl Smotricz
2009-12-18 12:12:04
Did i misunderstand the question? Added some sample rows to show what i mean..
Brimstedt
2009-12-18 15:57:38
Sorry, I misunderstood your code. The `level < 3` looks good. But you may need to do a separate select for the first "id = 20" row, depending on what he means by "starting with".
Carl Smotricz
2009-12-19 04:10:12
This question could use some clarification ;-)
Brimstedt
2009-12-19 09:59:05
A:
SQL doesn't offer conditions like you are asking for. It might be easiest to simply SELECT * and do the filtering in code.
As long as you don't ORDER BY or something else that requires the entire result set to be fetched all at once, this should perform reasonably well; you stop processing after finding your first level < 3
and then the zillions of other possible results behind that are never fetched.
Carl Smotricz
2009-12-18 12:16:01
too bad that mysql doesnt support thisthanks for your advice and suggestions anyways
XzenTorXz
2009-12-18 12:45:13
This is possible entirely in SQL. Fetching all records would be extremely inefficient if only a small number of records are needed but all must be fetched.
Mark Byers
2009-12-18 13:14:08
@Mark Byers If you can you substantiate the claim in your first sentence then that would surely be the way to go. But I suspect at least one of us hasn't fully grasped the requirements.
Carl Smotricz
2009-12-18 14:05:50
You're also not understanding the difference between selecting and fetching. I can create a ResultSet from an unrestricted SELECT from a gigabyte database within less than a second. This ResultSet doesn't contain all the data, it wraps something like a cursor. It's not the selection that takes time but the actual fetching. So if the sought data is near the beginning of the data set, it can be pleasantly quick.
Carl Smotricz
2009-12-18 14:11:33
OK sorry, I missed the bit where you assume that you only need to fetch the first few records. I think he wants to group by ordnernr and then orderby something that he hasn't specified (perhaps either id or time). This and start/stop is possible in SQL but it's not at all easy to get it to perform well. I will try to write this query if a) the poster clarifies the question and b) the poster is not satisfied with your solution. If the poster feels your solution is good enough, that's fine by me, I just don't want him to think it's the only option - it's not.
Mark Byers
2009-12-18 19:15:03
Brimsted's solution is starting to look good to me. That may end up being a winner.
Carl Smotricz
2009-12-19 04:12:03