SQL Inner通过订单加入客户

时间:2014-12-06 02:57:22

标签: sql select join group-by having

如果我有两张桌子(其中一位顾客,他们的信息包括地址,姓名,电子邮件等)和另一份订单(订单编号,发货日期,订购该项目的客户名称),我怎么能显示订单少于3个的客户的电子邮件?

我知道我必须使用内部联接和一些别名,但我不确定如何继续。

谢谢!

到目前为止:

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);

2 个答案:

答案 0 :(得分:1)

我用SQL创建了完整的示例。只需运行查询即可创建数据库,表和存储过程“获取客户订单”。

两个“客户”表中的样本数据和“订单”表中的关系是“1个客户到多个订单”,因此在订单内有一个客户的外键,用于识别哪个客户做了命令。如此。

首先创建数据库,运行此查询。

Create DataBase [Customer_OrdersDB]

刷新服务器资源管理器,您将找到已创建该名称的数据库。 然后运行Query以创建存储过程和表。

    USE [Customer_OrdersDB]
GO
CREATE PROCEDURE [dbo].[GetCustomer_Mail] 
AS
BEGIN
select Email as Customer_Mail 
from Customers as cust inner join Orders as ord
on cust.CustomerId = ord.OrderCustomerId
group by(Email)
having COUNT(ord.OrderCustomerId) < 3
END

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Customers](
    [CustomerId] [int] IDENTITY(1,1) NOT NULL,
    [CustomerName] [nvarchar](50) NULL,
    [Address] [nvarchar](50) NULL,
    [Email] [nvarchar](50) NULL,
 CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED 
(
    [CustomerId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[Orders]    Script Date: 12/6/2014 5:19:11 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Orders](
    [OrderId] [int] IDENTITY(1,1) NOT NULL,
    [OrderDate] [datetime] NULL,
    [OrderNumber] [nvarchar](50) NULL,
    [OrderCustomerId] [int] NULL,
 CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED 
(
    [OrderId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET IDENTITY_INSERT [dbo].[Customers] ON 

INSERT [dbo].[Customers] ([CustomerId], [CustomerName], [Address], [Email]) VALUES (1, N'Ahmed', N'Cairo', N'Ahmed@Yahoo.Com')
INSERT [dbo].[Customers] ([CustomerId], [CustomerName], [Address], [Email]) VALUES (2, N'Ali', N'Paris', N'Ali@yahoo.com')
INSERT [dbo].[Customers] ([CustomerId], [CustomerName], [Address], [Email]) VALUES (3, N'Samir', N'UK', N'Samir@msn.com')
SET IDENTITY_INSERT [dbo].[Customers] OFF
SET IDENTITY_INSERT [dbo].[Orders] ON 

INSERT [dbo].[Orders] ([OrderId], [OrderDate], [OrderNumber], [OrderCustomerId]) VALUES (1, CAST(0x0000A2A600000000 AS DateTime), N'1234', 1)
INSERT [dbo].[Orders] ([OrderId], [OrderDate], [OrderNumber], [OrderCustomerId]) VALUES (2, CAST(0x0000A2C700000000 AS DateTime), N'555', 1)
INSERT [dbo].[Orders] ([OrderId], [OrderDate], [OrderNumber], [OrderCustomerId]) VALUES (3, CAST(0x00009CF100000000 AS DateTime), N'56d66', 1)
INSERT [dbo].[Orders] ([OrderId], [OrderDate], [OrderNumber], [OrderCustomerId]) VALUES (4, CAST(0x00009E9B00000000 AS DateTime), N'555we', 2)
INSERT [dbo].[Orders] ([OrderId], [OrderDate], [OrderNumber], [OrderCustomerId]) VALUES (5, CAST(0x0000A2A600000000 AS DateTime), N'1234', 1)
INSERT [dbo].[Orders] ([OrderId], [OrderDate], [OrderNumber], [OrderCustomerId]) VALUES (6, CAST(0x0000A2C700000000 AS DateTime), N'555', 1)
SET IDENTITY_INSERT [dbo].[Orders] OFF
ALTER TABLE [dbo].[Orders]  WITH CHECK ADD  CONSTRAINT [FK_Orders_Customers] FOREIGN KEY([OrderCustomerId])
REFERENCES [dbo].[Customers] ([CustomerId])
GO
ALTER TABLE [dbo].[Orders] CHECK CONSTRAINT [FK_Orders_Customers]
GO

然后,如果他/她订购的订单少于3个订单,则访问您希望获得客户电子邮件的存储过程。

  1. 转到SQL server中的Server / Object explorer。
  2. 选择名称为[Customer_OrdersDB]的数据库。
  3. 选择“Programbility”。
  4. 选择“StoredProcedures”。
  5. 右键单击存储过程“GetCustomer_Mail”,然后选择“执行”。

答案 1 :(得分:1)

试试这个:

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