<p>I think I've figured it out. The sourceId of the <code>craft_relations</code> table is the elementId of the custom matrix table. And the targetId of the <code>craft_content</code> table is the elementId of the `craft_content table.</p>

<p><img src="https://i.sstatic.net/uWuBm.png" alt="Table relationships"></p>

<p><strong>Code example</strong></p>

<p>Here's what I was trying to do. I needed to filter entries based on a field value in an Entries matrix field on another entry. Whew.</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'FIRSTCOOLTHING';

$query = craft()-&gt;elements-&gt;buildElementsQuery($criteria);

$query-&gt;join('matrixblocks matrixblocks', 'matrixblocks.ownerId = entries.id');
$query-&gt;join('matrixcontent_MYMATRIX matrixcontent_MYMATRIX', 'matrixcontent_MYMATRIX.elementId = matrixblocks.id');
$query-&gt;join('relations relations', 'relations.sourceId = matrixcontent_MYMATRIX.elementId');
$query-&gt;join('content SECONDCOOLTHING', 'SECONDCOOLTHING.elementId = relations.targetId');

$query-&gt;andWhere('SECONDCOOLTHING.field_FIELDHANDLE = '.$THINGTOSEARCHFOR);

$query-&gt;order('title asc');

$queryResults = $query-&gt;queryAll();
return EntryModel::populateModels($queryResults);
</code></pre>

CC BY-SA 3.0
2015-08-30T04:03:07.613Z
2015-08-30T04:03:07.613Z
ashley
<p>All relational fields (Entries, Assets, Categories, Tags, etc.) store their relationship information in the database table <code>craft_relations</code> table.</p>
<p>There you can see the field ID, source and target ID as well as the source locale and sort order.</p>

CC BY-SA 3.0
2015-08-30T02:25:57.063Z
So, targetId is `craft_content.elementId`, and is the sourceId is `craft_matrixblocks.id`?
ashley
CC BY-SA 4.0
2015-08-30T00:53:57.253Z
2021-12-01T02:09:44.713Z
2021-12-01T02:09:44.713Z
Brad Bell
<p>I can see the simple matrix field data in the <code>craft_matrixcontent_MYMATRIX</code>table.</p>

<pre><code>craft_matrixcontent_MYMATRIX
---------------------------
id | elementId | locale | field_BLOCKTYPE_FIELDHANDLE | field_BLOCKTYPE_FIELDHANDLE | dateCreated | dateUpdated | uid
</code></pre>

<p>If I need to query upon those fields, I can thanks to some of the great posts to this site. But those fields are simple text fields.</p>

<p>I have an Entries field in my matrix. Where can I find this relationship in the database? It doesn't exist in the <code>craft_matrixcontent_MYMATRIX</code> table.</p>

<matrix><database>
Where are Matrix Entries Fields Relationships in the Database?
# Where are Matrix Entries Fields Relationships in the Database?

(Asked by: username_0 on 2015-08-29)

I can see the simple matrix field data in the craft_matrixcontent_MYMATRIXtable.
craft_matrixcontent_MYMATRIX
---------------------------
id | elementId | locale | field_BLOCKTYPE_FIELDHANDLE | field_BLOCKTYPE_FIELDHANDLE | dateCreated | dateUpdated | uid

If I need to query upon those fields, I can thanks to some of the great posts to this site. But those fields are simple text fields.
I have an Entries field in my matrix. Where can I find this relationship in the database? It doesn't exist in the craft_matrixcontent_MYMATRIX table.

**Tags:** matrix, database


---

## 2 answers

### Answer by username_0 on 2015-08-30. Score: 2

I think I've figured it out. The sourceId of the craft_relations table is the elementId of the custom matrix table. And the targetId of the craft_content table is the elementId of the `craft_content table.

Code example
Here's what I was trying to do. I needed to filter entries based on a field value in an Entries matrix field on another entry. Whew.
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'FIRSTCOOLTHING';

$query = craft()->elements->buildElementsQuery($criteria);

$query->join('matrixblocks matrixblocks', 'matrixblocks.ownerId = entries.id');
$query->join('matrixcontent_MYMATRIX matrixcontent_MYMATRIX', 'matrixcontent_MYMATRIX.elementId = matrixblocks.id');
$query->join('relations relations', 'relations.sourceId = matrixcontent_MYMATRIX.elementId');
$query->join('content SECONDCOOLTHING', 'SECONDCOOLTHING.elementId = relations.targetId');

$query->andWhere('SECONDCOOLTHING.field_FIELDHANDLE = '.$THINGTOSEARCHFOR);

$query->order('title asc');

$queryResults = $query->queryAll();
return EntryModel::populateModels($queryResults);


---

### Answer by username_1 on 2015-08-30. Score: 1

All relational fields (Entries, Assets, Categories, Tags, etc.) store their relationship information in the database table `craft_relations` table.

There you can see the field ID, source and target ID as well as the source locale and sort order.

> **Comments:**
> **username_0**: So, targetId is `craft_content.elementId`, and is the sourceId is `craft_matrixblocks.id`?  

---

