Joining two plugin tables by reference Id causes duplicates I'm running a query in which I need to join two tables table_views and table_clicks. Both tables share several column names (mainly refId, month, year and total).

However when I try and join these tables together, instead of returning ~18 or 20 rows/records. It repeats the join for every single row, producing 360 records.

How can I query and combine both tables by their reference Id?

        $records = craft()-&gt;db-&gt;createCommand()
        -&gt;select('*')
        -&gt;from('table_views table_views')
        -&gt;leftJoin('table_clicks table_clicks', 'table_views.refId=table_clicks.refId')
        -&gt;where('table_views.refId = :refId', array(':refId' =&gt; $id))
        -&gt;query();
You're probably looking for an inner join (which Yii just calls a join)instead of a leftJoin: https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/