# How to use Inner Join..?

**URL:** https://discourse.cakephp.org/t/how-to-use-inner-join/5488
**Category:** Need Help
**Created:** [January 4, 2019, 12:05pm UTC](https://discourse.cakephp.org/t/how-to-use-inner-join/5488 "2019-01-04T12:05:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![itsdarshan](https://avatars.discourse-cdn.com/v4/letter/i/b487fb/32.png) [@itsdarshan](https://discourse.cakephp.org/u/itsdarshan)
#### Post date: [January 4, 2019, 12:05pm UTC](https://discourse.cakephp.org/t/how-to-use-inner-join/5488/1 "2019-01-04T12:05:46Z")

</div>

Hai,

Can anyone tell me shortly how to join (inner) two tables ,…?  
I referred documentation but i am not getting …

anyone Help me…

Regards,

---

<div class="post-metadata">

### Author: ![Diego](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/diego/32/296_2.png) [@Diego](https://discourse.cakephp.org/u/Diego)
#### Post date: [January 4, 2019, 12:18pm UTC](https://discourse.cakephp.org/t/how-to-use-inner-join/5488/2 "2019-01-04T12:18:13Z")

</div>

```
$data = $this->Model1->find('all')
        ->select(['id', 'name'])
        ->innerJoinWith('Model2', function ($q) use ($filter_status) {
            return $q->where(['Model2.status' => $filter_status]);
        });

```

or if you dont need the related model data:

```
$data = $this->Model1->find('all')
->matching('Model2');

```

you can use deep association notation too:

```
$data = $this->Model1->find('all')
->matching('Model2.Model3');

```

if you need retaled model data:

```
$data = $this->Model1->find('all')
->contain([
    'Model2',
    'Model5',
]);

```

all this depends on you doing the correct association in the table model

---

<div class="post-metadata">

### Author: ![itsdarshan](https://avatars.discourse-cdn.com/v4/letter/i/b487fb/32.png) [@itsdarshan](https://discourse.cakephp.org/u/itsdarshan)
#### Post date: [January 10, 2019, 7:20am UTC](https://discourse.cakephp.org/t/how-to-use-inner-join/5488/3 "2019-01-10T07:20:42Z")

</div>

Can u please tell me where can i add that code

sorry i am new to cakephp

---

<div class="post-metadata">

### Author: ![Diego](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/diego/32/296_2.png) [@Diego](https://discourse.cakephp.org/u/Diego)
#### Post date: [January 10, 2019, 11:07am UTC](https://discourse.cakephp.org/t/how-to-use-inner-join/5488/4 "2019-01-10T11:07:05Z")

</div>

in the Controller function

---

<div class="post-metadata">

### Author: ![dakota](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dakota/32/1421_2.png) [@dakota](https://discourse.cakephp.org/u/dakota)
#### Post date: [January 10, 2019, 12:17pm UTC](https://discourse.cakephp.org/t/how-to-use-inner-join/5488/5 "2019-01-10T12:17:40Z")

</div>

@Diego Couple of small corrections 🙂

> [@Diego](#):
>
> $data = $this-\>Model1-\>find(‘all’) -\>select([‘id’, ‘name’]) -\>innerJoinWith(‘Model2’, function ($q) use ($filter\_status) { return $q-\>where([‘Model2.status’ =\> $filter\_status]); });

Will only work if they are related, and will not include the related data in the result entities.

> [@Diego](#):
>
> $data = $this-\>Model1-\>find(‘all’) -\>matching(‘Model2’);

Does include the related data (in the `_matchingData ` property)

To do an inner join without any association existing, you can use the `innerJoin()` query method.

@itsdarshan

There is quite a lot of information available in the book. [Associations - Linking Tables Together - 3.10](https://book.cakephp.org/3.0/en/orm/associations.html) and [Query Builder - 3.10](https://book.cakephp.org/3.0/en/orm/query-builder.html#loading-associations) are good starting points.

I also highly recommend taking part in the nearly free interactive training course: [https://training.cakephp.org](https://training.cakephp.org) and watching some of the past CakeFest workshop videos on [youtube](https://www.youtube.com/cakephp)
