Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend how do I create a left join

How do I turn this left join query:

select advertisercontest.*, advertiseraccount.advertiserid, advertiseraccount.companyname
from advertisercontest
left join advertiseraccount on advertiseraccount.loginid = advertisercontest.loginid 
where advertisercontest.golive is not NULL;

into a left join in Zend?

like image 389
coder3 Avatar asked Feb 10 '11 09:02

coder3


People also ask

What is the syntax of left join?

LEFT JOIN SyntaxON table1.column_name = table2.column_name; Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.

What is left joining?

The LEFT JOIN command returns all rows from the left table, and the matching rows from the right table. The result is NULL from the right side, if there is no match.

How do you replace left JOINs?

The conditions in the ON clause of a LEFT JOIN , when unmet, join the first table's row with null values replacing a row from the second table. If those conditions appear in the WHERE clause, they exclude the first row when unmet. This effectively converts your LEFT JOIN into an ordinary inner JOIN .


1 Answers

You could do as follows:

    $db = Zend_Db_Table::getDefaultAdapter();

    $select = $db->select();
    $select->from('advertisercontest', '*')
            ->joinLeft(
                    'advertiseraccount',
                    'advertiseraccount.loginid = advertisercontest.loginid',
                    array('advertiseraccount.advertiserid', 'advertiseraccount.companyname')
                    )
            ->where('advertisercontest.golive is not NULL');;

    $result = $db->fetchAll($select);

    var_dump($result);

Here is the Zend_Db_Select documentation.

like image 67
Marcin Avatar answered Oct 16 '22 15:10

Marcin