我现在有两个表,一个是用户信息表tb_userinfo,另一个是消费明细表tb_mingxi,其中通过,两个表都有一个m_id的外键。我现在想查询的是在某一个月份中,没有消费的用户的详细信息(这个在用户信息表中)及在这个月之前该用户最后一次消费的日期(日期都在消费明细表中)。今天想了很多办法都没管用,求教各位达人了!!!

解决方案 »

  1.   

    没有消费的用户的详细信息Select * From  tb_userinfo u
    Where Not Exists (Select m.Id form mingxi m
                      Where m.Id = u.Id
                      And  .....)--其它条件                  
    然后根据用户id找最大的日期,可能会用到子查询,有没有效率更高点的?
      

  2.   

    select
        a.m_id
       ,min(b.deal_date)
      from tb_userinfo a
      left out join tb_mingxi b
        on a.m_id = b.m_id
     where b.deal_date < $statis_date
     group by
        a.m_id
    这样行吗?
      

  3.   

    select A.*, B.max_date 
    from  tb_userinfo A
    inner join (
                select m_id,max(date) as max_date from tb_mingxi
                where date < 指定日期
                group by m_id, date
               ) B
    on A与B的关联条件
      

  4.   

    刚才发错了:select A.*, B.max_date 
    from  tb_userinfo A
    inner join (
                select m_id,max(date) as max_date from tb_mingxi C
                where date < 指定日期 and not exists (select 1 from tb_mingxi where m_id = C.m_id and date = 指定日期)
                group by m_id, date
               ) B
    on A与B的关联条件