如何删除相同的子查询,之后我想要相同的结果

时间:2015-08-06 09:42:44

标签: mysql sql sqlite

我有一个查询得到了访问者的结果。不使用子查询。

如何在此查询中获取上次访问者的日期

select u.id,
    u.fname,
    u.lname,
    CONCAT_WS(' ',u.fname,u.lname) as fullname,
    u.gender,
    TIMESTAMPDIFF(YEAR, u.bdate, CURDATE()) as age,
    u.username,
    g.profile_image,
    v.userid,
    v.visitor_id,
    v.visited_time 
    from tbl_user u 
    inner join (
        select * from tbl_visitors order by visited_time desc 
    )as v on(v.visitor_id=u.id) 
    left join tbl_gallery_master g on(u.profile_image=g.id) 
    group by v.userid,v.visitor_id 
    order by v.visited_time desc

在这个查询中我得到了绝对精美的记录,但我不想要子查询

2 个答案:

答案 0 :(得分:0)



select u.id,u.fname,u.lname,CONCAT_WS(' ',u.fname,u.lname) as fullname,
	  u.gender,TIMESTAMPDIFF(YEAR, u.bdate, CURDATE()) as age,
	  u.username,g.profile_image,v.userid,v.visitor_id,v.visited_time 
from tbl_user u 
inner join tbl_visitors as v  on(v.visitor_id=u.id) 
left join tbl_gallery_master g on(u.profile_image=g.id) 
group by v.userid,v.visitor_id 
order by v.visited_time desc




答案 1 :(得分:0)

如果你的查询有效,那么它也应该起作用:

select u.id,
u.fname,
u.lname,
CONCAT_WS(' ',u.fname,u.lname) as fullname,
u.gender,
TIMESTAMPDIFF(YEAR, u.bdate, CURDATE()) as age,
u.username,
g.profile_image,
v.userid,
v.visitor_id,
v.visited_time 
from tbl_user u 
inner join  tbl_visitors v on v.visitor_id=u.id
left join tbl_gallery_master g on u.profile_image=g.id 
group by v.userid, v.visitor_id 
order by v.visited_time desc