mysql - SELECT UNTIL in sql

前端 未结 3 1091
慢半拍i
慢半拍i 2020-12-11 10:55

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
----         


        
相关标签:
3条回答
  • 2020-12-11 11:24

    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

    0 讨论(0)
  • 2020-12-11 11:26

    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

    0 讨论(0)
  • 2020-12-11 11:29

    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.

    0 讨论(0)
提交回复
热议问题