How to Get Distinct Entries from Joined Tables Through Join()

What you could consider doing is adding a group by clause for the visitor_id column.

$collection = Mage::getModel('log/visitor')->getCollection()->addFieldToFilter('store_id', array('eq' => 1));
$collection->getSelect()
    ->join(
        array('log_customer'=> $collection->getTable('log/customer')),
        'main_table.`visitor_id`= log_customer.`visitor_id`'
    )->group('main_table.visitor_id');

But you might also need to add an order by to make sure that you get the newest results first.


Zend_Db_Select::distinct()

The above method should help you. You could just call it by:

$collection->getSelect()->distinct();

Distinct and Group, as described by David and Paras, will work on 9 out of 10 queries. If it doesn't, make sure that you aren't reusing the column name of the primary column of the collection.

The following does not work all the time because catalog_product_entity has a column named entity_id which is the primary column of the order collection

$order_collection = Mage::getModel('sales/order')
        ->getCollection();

    $product_collection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addFieldToFilter('attribute_set_id', $attributeSetId)
    ;

    $order_collection->getSelect()
        ->join($sales_order_item_table,
            "$sales_order_item_table.order_id = main_table.entity_id")
        ->join(new Zend_Db_Expr('('.$product_collection->getSelect().')'),
            "t.entity_id = $sales_order_item_table.product_id")
        ->group("main_table.entity_id")
    ;

To fix it, just add an empty array as the third parameter of the join

        $order_collection->getSelect()
        ->join($sales_order_item_table,
            "$sales_order_item_table.order_id = main_table.entity_id",
            array())
        ->join(new Zend_Db_Expr('('.$product_collection->getSelect().')'),
            "t.entity_id = $sales_order_item_table.product_id",
            array())
        ->group("main_table.entity_id")
    ;