Where are Matrix Entries Fields Relationships in the Database?
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()-&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);