0

我编写了 sql 请求来检索 joomla 文章以及作者:

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;
    }

它就像一个魅力,但这里有一些奇怪的部分,如果我从不同的浏览器登录同一个用户,它会复制我的所有内容两次,如果我第三次登录,它会复制所有内容 3 次,依此类推!据我所知,问题出在这一行:

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

有什么建议么?

4

2 回答 2

1

你可能想给这个 user_id 添加一个别名,这样数据库就知道这个 user_id 指的是哪个表。

LEFT JOIN #__comprofiler AS ua ON user_id = a.created_by

我会运行较小版本的查询,看看何时开始引入重复项。从前 3 个表连接开始。如果您没有看到重复项,请再添加一个连接,因此您将连接查询中的前四个表。将有助于准确找到问题的出现位置。

于 2013-03-13T16:15:22.813 回答
0

它重复是因为从另一个浏览器登录会为该用户创建一个新会话。试着做

SELECT DISTINCT
...
于 2013-03-13T17:04:09.323 回答