Search code examples
sqlsessionjoomla

Sql request for some reason duplicates all content


I wrote sql request to retrieve joomla articles along with authors:

static function getLists(&$params)
    {
        $db     = JFactory::getDbo();
        $lists  = null;
        $query = "SELECT cc.title AS category, a.id, a.title, a.created as created, av.add_image as imgthrumb, ua.avatar as avatar, ar.intotext as introtext, ss.userid as onlineid,
        u.id AS userlink, u.username as username, u.lastvisitDate as lastvisit, a.hits, cc.id as catid, a.state as state, a.created_by as authorid, mp.group_id as groupid, ug.title as ugtitle
        FROM #__content AS a
        LEFT JOIN #__categories AS cc ON a.catid = cc.id
        LEFT JOIN #__users AS u ON u.id = a.created_by
        LEFT JOIN #__cck_store_item_content AS av ON av.id = a.id
        LEFT JOIN #__comprofiler AS ua ON user_id = a.created_by
        LEFT JOIN #__cck_store_item_cck_store_item_content AS ar ON ar.id = a.id
        LEFT JOIN #__user_usergroup_map AS mp ON mp.user_id = a.created_by
        LEFT JOIN #__usergroups AS ug ON ug.id = mp.group_id
        LEFT JOIN #__session AS ss ON ss.userid = u.id AND ss.userid > 0 AND ss.guest = 0
        WHERE a.created_by = u.id  AND a.id >0
        AND DATE(a.created) >= DATE_SUB(CURRENT_DATE, INTERVAL ".(int)$safe_date." DAY) AND a.access = 1 AND a.state = 1 AND a.created > 0
        ORDER BY created desc";
        $db->setQuery($query,0,10);
        $lists = $db->loadObjectList();         

        return $lists;
    }

It works like a charm, but heres some strange part, if I login same user from different browser, it duplicates all of my content twice, and if I login for the third time, it duplicates all content 3 times and so on! As far as I can see the problem is in this line:

LEFT JOIN #__session AS ss ON ss.userid = u.id AND ss.userid > 0 AND ss.guest = 0

any suggestions?


Solution

  • It duplicates because logging from another browser creates a new session for that user. Try to do

    SELECT DISTINCT
    ...