1.注意是否要带上标识如(userId)
2.如果没有查询条件,如select * from xxx,delete from xxx 建议在后面加where 1=1;
根据用户group_wxid分组查询出最新的数据
这里一个wxid会可能会对应多个name,我们所需的是最新的名字,期望结果如下:
1.先用max(sender_time) 查询出最新数据,并根据用户id分组
select group_wxid, max(sender_time) as sender_time from T_W_EVENT group by group_wxid
2.使用inner join 按条件查询出所需字段信息
select t.group_wxid,t.group_name
from T_W_EVENT t
inner join
(select group_wxid, max(sender_time) as sender_time from T_W_EVENT group by group_wxid) a
on a.group_wxid=t.group_wxid
and a.sender_time = t.sender_time
and t.user_id=29
and t.is_deleted=0
上下合并两张表
可以使用union或者union all
将表格需要上下合并的查找出来,然后别名修改成相同的就可以改别名让他们相同,然后就可以合并了。 另一张表没有的字段用null 或者''代替
如:select col1,col2,null as col3 from t1 where ****
union
select col1,col2,col3 from t2 where ****