Hello!

There is a table in the database (example):

id| f1 | f_new | f_alt | status 1 | 13 | | | active 2 | 12 | 13 | | archive 

And there is a request (example)

 SELECT * FROM `table` WHERE f1 = '12' or f_alt = '12' 

The question is: is it possible in the request to find out that the line has a non-empty f_new field and then select the line by value from f_new , i.e. f_new = f1 ( id1 ), and do not select the row id2

UPD. There is a problem if we suppose the table looks like this (you need to get the active drain, i.e. id3 :

 id| f1 | f_new | f_alt | status 1 | 13 | 14 | | archive 2 | 12 | 13 | | archive 3 | 14 | | | active 

    1 answer 1

     select * from table where f1=(select f_new from table where f1=12) 

    A very simple option that does not take into account, for example, that the values ​​in the fields are not unique.

    • Look please, updated, there is a problem - ka5itoshka
    • select * from table where status = 'active' or did I misunderstand the question? - rjhdby
    • @ ka5itoshka If you need to run the whole chain f1 = 12, f_new = 13-> f1 = 13, f_new = 14-> f1 = 14, f_new = null, then you will not be able to use simple SQL here. We'll have to write a recursive function, either stored or in code. - rjhdby
    • get along then. Thanks again! - ka5itoshka