我有2个表,我想在我的网站列表中显示所有用户,并从表tbl_login_details
中显示他的上次登录详细信息,但获得该用户的上次登录详细信息
1-从表中显示所有用户-tbl_user
2-为所有用户显示LAST登录,因此tbl_login_details
的数据必须是ORDER然后采用LIMT
---------tbl_user-------
id user_id user_name
1 5 mohammed
2 7 ahmed
-------------tbl_login_details----------
id user_id last_activity
1 7 2016-5-2
2 7 2017-4-2
3 7 20-17-8-4
我的工作是
$this->db->select('m.*,u.*');
$this->db->from('tbl_user m');
//$this->db->where("m.user_id", $id);
$this->db->join('tbl_login_details u ', 'u.user_id = m.user_id');
$where = 'm.user_id = u.user_id OR u.user_id = m.user_id OR m.user_id=" " ORDER last_activity';
$where = $this->db->order_by("last_activity", "desc");
$this->db->where($where);
// $this->db->limit('1');
$this->db->group_by('m.user_id');// add group_by
$query = $this->db->get();
return $query->result();
答案 0 :(得分:0)
使用最大聚合找到上一个活动:
select userid, max(last_activity)
from tbl_user a inner join tbl_login_details b on a.userid=b.userid
答案 1 :(得分:0)
您是否尝试从每个用户那里获取最新的详细信息登录信息,如果可以,则可以使用ORDER DESC AND LIMIT 1
创建查询以首先显示tbl_user
SELECT * FROM tbl_user ORDER BY id ASC
然后您可以创建查询以获取上次用户登录
SELECT * FROM tbl_login_details WHERE user_id = 7 ORDER BY id DESC LIMIT 1
您可以使用模式弹出窗口进行显示详细信息登录。
还是您尝试这样显示?
id user_id user_name last_activity
1 5 mohammed 20-17-8-4
2 7 ahmed 20-17-8-4
答案 2 :(得分:0)
可能您正在搜索关注
SELECT user_id, user_name, (SELECT last_activity FROM tbl_login_details WHERE tbl_login_details.user_id = tbl_user.user_id ORDER BY last_activity DESC LIMIT 1) as last_activity FROM tbl_user
更新的代码。搜索如何在活动记录中编写此子查询。或仅使用自定义查询,例如$ query = $ this-> db-> query(“ YOUR QUERY”);
答案 3 :(得分:0)
帮助CI最大聚合以查找上一个活动:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\timregan\source\MentalHealth\code\preprocessing\feat_transform.py", line 171, in filter_anevexp
anevexp_df = anevexp_df[anevexp_df["user_id"].isin(df)].copy()
File "C:\Users\timregan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 2682, in __getitem__
return self._getitem_array(key)
File "C:\Users\timregan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 2724, in _getitem_array
return self._take(indexer, axis=0)
File "C:\Users\timregan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 2789, in _take
verify=True)
File "C:\Users\timregan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals.py", line 4539, in take
axis=axis, allow_dups=True)
File "C:\Users\timregan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals.py", line 4425, in reindex_indexer
for blk in self.blocks]
File "C:\Users\timregan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals.py", line 4425, in <listcomp>
for blk in self.blocks]
File "C:\Users\timregan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\internals.py", line 1258, in take_nd
allow_fill=True, fill_value=fill_value)
File "C:\Users\timregan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\algorithms.py", line 1655, in take_nd
out = np.empty(out_shape, dtype=dtype)
MemoryError
答案 4 :(得分:0)
这是获取该代码的简单SQL代码。您可以通过CodeIgniter的方式来实现: 在CodeIgniter中实现此SQL。
SELECT tbl_user.user_id, tbl_user.user_name, tbl_login_details.user_id, tbl_login_details.last_activity FROM tbl_user, tbl_login_details WHERE tbl_user.user_id = tbl_login_details.user_id ORDER BY tbl_login_details.last_activity DESC
答案 5 :(得分:0)
希望这可以为您提供帮助
SELECT u.id,u.user_id,u.user_name,MAX(l.last_activity) AS last_activity
FROM tbl_user u
LEFT JOIN tbl_login_details l ON(u.user_id=l.user_id)
GROUP BY u.user_id
在codeigniter自定义查询生成器上
$query = $this->db->query("SELECT u.id,u.user_id,u.user_name,MAX(l.last_activity) AS last_activity FROM tbl_user u LEFT JOIN tbl_login_details l ON(u.user_id=l.user_id) GROUP BY u.user_id");
foreach ($query->result() as $row)
{
echo $row->id;
echo $row->user_id;
echo $row->user_name;
echo $row->last_activity ;
}