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()->db->createCommand()
        ->from('table_views table_views')
        ->leftJoin('table_clicks table_clicks', 'table_views.refId=table_clicks.refId')
        ->where('table_views.refId = :refId', array(':refId' => $id))
  • Leaving this up as a valid question but this was certainly user error. The query ran as intended but because we ran the where looking for Id, it looped through each time because we didn't re-define the month/year. – Adam McCombs Jan 27 '17 at 21:47

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/


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.