3

I want to select rows until I found a certain value and it's not numeric so I cannot use > or <. How can I do this ?

Example :

-----
Value
-----
45
434
348
213
543
3445
343
123
34345

I want to select all records before 543 (NOT ORDERED BY anything)

Thanks in advance

12
  • 2
    Do give an example of sample data and what you'd like to achieve on it. Commented Oct 22, 2012 at 20:41
  • 4
    A table is not an array, it has not an inherent order. So what column(s) do you want to order by? Commented Oct 22, 2012 at 20:46
  • 1
    You can't select records before 543 not ordered by anything. As @TimSchmelter said, there is no inherent order on a table Commented Oct 22, 2012 at 20:56
  • 1
    not so much impossible as illogical. (assuming we're understanding the question correctly) Commented Oct 22, 2012 at 20:58
  • 1
    If you want both the before (which implies an order), and the not ordered by (which requires not to be an order), you see that the intersection of the two sets of ways in which it can be done is empty. You need to do this in a language other than SQL (and by doing this, you will imply an order -- the order of extraction). Commented Oct 22, 2012 at 21:00

3 Answers 3

6

This answer might be late, but since it's the first result on google with term of 'mysql select until' hope this might help someone.

What you can do is declare a marker and if you found the value needed, set the marker to any value other than NULL. Then we can use ISNULL for checking if we have reached the row.

SET @marker = NULL;
SELECT value FROM table1 WHERE ISNULL(@marker:=IF(value=543,value,@marker));

Here is a demo

Sign up to request clarification or add additional context in comments.

1 Comment

While this may return the desired result, I suspect it will perform a full table or index scan: most likely the optimizer does not realize that the condition will never become true again once it is false, and thus continue to load every remaining row and evaluate the condition, only that exclude that row again. For large data sets, that wastes a lot of effort. (Let me know if there is an optimization I am unaware of.)
5

Having cleared the problem of "not there being an order", that is, you will have to choose some kind of ordering (for example the order of primary key, or the order in which the records were assigned OIDs), your question becomes:

"I want all the records whose ordering field is less than the value
 of the row for which ordering field is minimum, and VALUE is 543".

So you select the minimum row for which the value is 543

SELECT MIN(id) AS id FROM yourtable WHERE value = 543

or maybe, for other purposes, something like

SELECT MIN(id) AS id FROM yourtable WHERE value >= 543

and then select all records before that one:

SELECT value FROM yourtable WHERE id < ( SELECT MIN(id) AS id FROM yourtable WHERE value = 543 );

So your table could be:

ID    Value
-----------
1     45
2     434
3     348
4     213
5     543
6     3445
7     343
8     123
9     34345

and you would get:

45
434
348
213
543

You don't see the order, but there must be one. Here it is the order on id.

As you requested, value can be whatever - a text, or even an image BLOB - and the "ordering" stays hidden.

In some other language you would do something like:

qry = SQL.exec('SELECT * FROM yourtable;');
while ((value = qry.next().value) != '543':
    write value '\n'
qry.close()

but then either the SELECT's order would not be specified, and you might get every time a different result, or it would be the order of tuple insertion in the table. Which is the same as using id explicitly.

1 Comment

@Fady What do you want to happen if 543 appears more than once in your values?
-5

You can just use an equality comparator in the query, to directly search the value you're looking for.

Why would you want to loop through your database until you found it when you can just specify what you're searching for ?

You didn't give an example of what value it is you're searching for, so if it is a string

Select x.* from table x WHERE x.Column = 'String Value'

However, if you do want to put some extra load on your database (which it is in this undefined case), you'd have a stored procedure, looping through ID's stored in the database.

For that, take a look at the following post, which might give you some inspiration : SQL LOOP INSERT Based on List of ID's

1 Comment

This doesn't really answer what was asked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.