I am facing issue in finding the data from MySql table.

**Table A:**

```
+-------+-------------+------+-----+-------------------+-------+
| ID    | Table_b_fk  |Value | age | name              | score |
+-------+-------------+------+-----+-------------------+-------+
| 01    | 01          | 255  |  21 | Tom               |  65   | 
| 02    | 02          | 36   |  20 | Peter             |  95   | 
| 03    | 03          | 25   |  22 | John              |  65   | 
| 04    | 04          | 36   |  20 | Bond              |  95   |<<----First 
| 05    | 05          | 258  |  22 | Smith             |  65   | 
| 06    | 06          | 420  |  20 | Robert            |  95   | 
| 07    | 07          | 258  |  22 | Nisarg Patel      |  65   | 
| 08    | 08          | 16   |  21 | Peter             |  95   | 
| 09    | 09          | 25   |  23 | J0k               |  65   | 
| 10    | 10          | 36   |  22 | Prodigitalson     |  95   | 
| 11    | 11          | 205  |  22 | Silver            |  65   |<<----Next
| 12    | 12          | 37   |  20 | Json              |  95   | 
| 13    | 13          | 285  |  23 | Villa             |  65   | 
| 14    | 14          | 36   |  22 | Parker            |  95   | 
+-------+-------------+------+-----+-------------------+-------+

```

**Table B:**

```
+-------+-------------+------+-----+-------------------+-------+
| ID    | Result      | M1   |  M2 | name              | score |
+-------+-------------+------+-----+-------------------+-------+
| 01    | Pass        | 30   |  26 | Tom               |  65   | 
| 02    | Pass        | 30   |  20 | Peter             |  95   | 
| 03    | Pass        | 25   |  60 | John              |  65   | 
| 04    | Pass        | 100  | 100 | Bond              |  95   |<<----First 
| 05    | Pass        | 55   |  65 | Smith             |  65   | 
| 06    | Pass        | 80   |  95 | Robert            |  95   | 
| 07    | Pass        | 65   |  75 | Nisarg Patel      |  65   | 
| 08    | Pass        | 56   |  71 | Peter             |  95   | 
| 09    | Pass        | 90   |  96 | J0k               |  65   | 
| 10    | Pass        | 96   |  96 | Prodigitalson     |  95   | 
| 11    | Pass        | 100  | 100 | Silver            |  65   |<<----Next
| 12    | Pass        | 47   |  92 | Json              |  95   | 
| 13    | Pass        | 82   |  73 | Villa             |  65   | 
| 14    | Pass        | 86   |  72 | Parker            |  95   | 
+-------+-------------+------+-----+-------------------+-------+

```

I am joining TableA & TableB, where in TableA `Table_b_fk` is foreign key to TableB.

I am finding the record which matches the TableB column `M1 & M2 = 100`.

**My Scenario: 1**

I know the first occurrence of the match record ID : `04` in `TableA`. I want to do a search to find the next record with `M1 & M2 = 100`. (Record Id-11) But the search should not start from **01**. It should start from the last found record Id. That is from **O4** the search should start to find the next occurrence of the record.

My Try:
I tried to find using Limit but it didn't help me to find. Can some one help me in this?

**Edit: 1**

**My Scenario: 2**
In my second case my TableB has repeated Data and the ID was foreign in TableA. How can I fins the record. ? with the matching ID/M1/M2 values: I found a solution for that. I just want to find the Current Record FOREIGN KEY and Check for the next occurrence of the record in the same table and I can get the next record rite?

In this case my TableB record are not as same as TableA records. In other words my TableA records will point to tableA. Many-to-one. Is this rite?

**Edit: 2**

Thanks for all your efforts and knowledge I found a solution for scenario:2 check it:

```
CREATE TABLE TableB (
  ID Int,
  Result VARCHAR(20),
  M1 INT,
  M2 INT,
  name VARCHAR(20),
  Score INT);

INSERT INTO TableB VALUES
( 11    , 'Pass'        , 30   ,  26 , 'Tom'               ,  65   ), 
( 13    , 'Pass'        , 30   ,  20 , 'Peter'             ,  95   ),
( 80    , 'Pass'        , 25   ,  60 , 'John'              ,  65   ),
( 81    , 'Pass'        , 100  , 100 , 'Bond'              ,  95   ),
( 90    , 'Pass'        , 55   ,  65 , 'Smith'             ,  65   ),
( 96    , 'Pass'        , 80   ,  95 , 'Robert'            ,  95   ),
( 97    , 'Pass'        , 65   ,  75 , 'Nisarg Patel'      ,  65   ),
( 98    , 'Pass'        , 56   ,  71 , 'Peter'             ,  95   ),
( 99    , 'Pass'        , 90   ,  96 , 'J0k'               ,  65   ),
( 100    , 'Pass'        , 96   ,  96 , 'Prodigitalson'     ,  95   ),
( 101    , 'Pass'        , 10  , 10 , 'Silver'            ,  65   ),
( 103    , 'Pass'        , 47   ,  92 , 'Json'              ,  95   ),
( 201    , 'Pass'        , 82   ,  73 , 'Villa'             ,  65   ), 
( 222    , 'Pass'        , 86   ,  72 , 'Parker'            ,  95   )
;

CREATE TABLE TableA
    (`ID` int, `Table_b_fk` int, `Value` int, `age` int, `name` varchar(13), `score` int)
;

INSERT INTO TableA
    (`ID`, `Table_b_fk`, `Value`, `age`, `name`, `score`)
VALUES
    (01, 11, 255, 21, 'Tom', 65),
    (02, 81, 36, 20, 'Peter', 95),
    (03, 80, 25, 22, 'John', 65),
    (04, 97, 36, 20, 'Bond', 95),
    (05, 81, 258, 22, 'Smith', 65),
    (06, 06, 420, 20, 'Robert', 95),
    (07, 81, 258, 22, 'Nisarg Patel', 65),
    (08, 08, 16, 21, 'Peter', 95),
    (09, 96, 25, 23, 'J0k', 65),
    (10, 101, 36, 22, 'Prodigitalson', 95),
    (11, 222, 205, 22, 'Silver', 65),
    (12, 12, 37, 20, 'Json', 95),
    (13, 201, 285, 23, 'Villa', 65),
    (14, 101, 36, 22, 'Parker', 95)
;

```

Solution for that is:

```
SELECT a.id  
FROM TableB b
INNER JOIN TableA a 
ON a.Table_b_fk = b.id
WHERE M1 = 100 and M2 = 100  AND a.ID>4 limit 1

```

where the limit just limits the next record.. (answer is 5).

I case of `Doctrine 2`: Use the below Query code.

```
$qry = $this->manager()->createQueryBuilder()
        ->select(array('e', 's'))
        ->from('YOUR_DOMAIN', 'e')
        ->Join('e.table_b_k', 's')
        ->where("s.m1 = ?", $valueone)
        ->andwhere("s.m2 = ?", $valuetwo)
        ->andwhere("e.id > ?", $currentrecord)
        ->setMaxResult(1);

```

Note: `YOUR_DOMAIN` here is the TableA. TableA and TableB should be joined through the Mapping so we dont need to Join/Reference int he Query.. directly as TableB. The will be done by the second line Join in the above example. It is not tested as of now.