SQL Inner用订单加入客户 [英] SQL Inner Join customers with orders

查看:108
本文介绍了SQL Inner用订单加入客户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两张桌子(其中一个客户,其信息包括地址,姓名,电子邮件等)和另一个订单(订单号,发货日期,订购该商品的客户名称),我如何显示有少于3个订单的客户的电子邮件?

我知道我必须使用内部连接和一些别名,但我不知道如何继续。 / b>

谢谢!

到目前为止:

 选择customer.email 
从customer作为cust
INNER JOIN(选择customer_id,sum(line_qty)AS从订单中总计
ON ON cust。 customer_id = o.customer_id
where total =(SELECT total <3
FROM(select customer_id,sum(line_qty)AS total
from order as o cust.customer_id = o.customer_id
)as sub);


解决方案

这:

  SELECT c.email 
FROM customer AS c
LEFT OUTER JOIN命令AS o ON c .customer_id = o.customer_id
GROUP BY c.email
HAVING SUM(o.line_qty)< 3


if I have a two tables (one of customers, with their information including address, name, emails etc) and another of orders (with order number, shipping date, customer name who ordered that item), how could I show the email of the customers who have less than 3 orders?

I know I have to use an inner join and some alias's, but i'm not sure how to proceed.

Thanks!

what I have so far:

SELECT customer.email 
FROM customer as cust 
INNER JOIN (select customer_id, sum(line_qty) AS total 
            from orders as o ON cust.customer_id = o.customer_id 
            where total = (SELECT total < 3 
                           FROM (select customer_id, sum(line_qty) AS total 
                                 from orders as o ON cust.customer_id = o.customer_id
                                ) as sub);

解决方案

Try this:

SELECT c.email 
FROM customer AS c 
LEFT OUTER JOIN orders AS o ON c.customer_id = o.customer_id 
GROUP BY c.email 
HAVING SUM(o.line_qty) < 3

这篇关于SQL Inner用订单加入客户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆