Mysql查询查找两个日期之间有0个订单的客户

时间:2018-01-19 21:49:58

标签: mysql

参见SqlFiddle:enter link description here

两张桌子

  1. 客户
  2. 订单
  3. 如果在日期1和日期2之间找到所有客户的订单,即使他们有0个订单?

    这是我到目前为止所做的工作:

    select c.customer_name
         , c.date_created
         , o.grandtotal
         , o.order_date
      from Customers c
      left 
      join Orders o 
        on c.customer_id = o.customer_id
     where exists (select o1.* from Orders o1) 
       and c.customer_id in (1, 2, 3, 4)
     group 
        by c.customer_name
    

    我需要按日期订购:

    select c.customer_name, c.date_created, o.grandtotal, o.order_date
    from Customers c
    left join Orders o on c.customer_id = o.customer_id
    where exists (select o1.* from Orders o1) 
    and o.order_date between '2018-01-01' and '2018-01-18'
    and c.customer_id in (1, 2, 3, 4)
    group by c.customer_name
    

2 个答案:

答案 0 :(得分:3)

这应该这样做。如果您想查看所有特定客户及其订单。

t = [ [ 0,0,0]
      [ 0,0,0]
      [ 0,0,0] ]

c = [ 0, 1, 2 ]

v = [ 1, 10, 1000 ]

i = 1

# then f(t,c,v,i) where f is the broadcast function
f(t,c,v,i) outputs:

t = [ [ 10,  0,  0 ]
      [  0, 10,  0 ]
      [  0,  0, 10 ] ]

答案 1 :(得分:-1)

根据您提供的超级有限信息,我很确定您正在尝试进行LEFT JOIN。看看http://www.dofactory.com/sql/left-outer-join,看一个听起来与你所描述的很相似的例子。