我是SQL的新手,但基本上我正在尝试获取客户数据,一切正常,除非我想在每次导致问题时为最终结果添加电子邮件地址。
这适用于基于体积的网站。
我不确定我的错误是我的代码
SELECT
Orders.OrderID,
Orders.OrderDate,
Orders.ShipFirstName,
Customers.EmailAddress
FROM
Orders
JOIN
Customers ON customers.customerid = orders.customerid
WHERE
Orders.OrderStatus = 'Shipped'
AND Orders.ShipDate >= DATEADD(DAY, 20, GETDATE())
AND Orders.ShipDate <= DATEADD(DAY, -5, GETDATE())
UNION ALL
INSERT INTO Customers (EmailAddress)
VALUES ('Testemail@gmail.com');
ORDER BY Orders.OrderID desc
我想要做的就是这样,每次我运行这个,最后添加我的电子邮件地址,因为我们向所有导出信息的人发送订单更新,我想确保我得到一个该电子邮件的副本,无需在每封电子邮件中自我控制。
我实际上并不想在真实表格中添加任何内容,只是为了我的结果
例如,现在当我运行这个时,我得到了这个结果
3104 10/9/2014 12:54:00 PM Jeanine jrgtnce@yahoo.com
3065 10/6/2014 1:01:00 PM Annamarie the.annamarie@gmail.com
But I want this
3104 10/9/2014 12:54:00 PM Jeanine jrgtnce@yahoo.com
3065 10/6/2014 1:01:00 PM Annamarie te.annrie@gmail.com
null nulll null testemail@gmail.com
testemail@gmail.com将更改为我的个人电子邮件
答案 0 :(得分:3)
我假设您不是每次运行查询时都不想将相同的数据添加到Customers
,而只是将合成记录添加到结果中:
SELECT
Orders.OrderID,
Orders.OrderDate,
Orders.ShipFirstName,
Customers.EmailAddress
FROM
Orders
JOIN
Customers ON customers.customerid = orders.customerid
WHERE
Orders.OrderStatus = 'Shipped'
AND Orders.ShipDate >= DATEADD(DAY, 20, GETDATE())
AND Orders.ShipDate <= DATEADD(DAY, -5, GETDATE())
UNION ALL
SELECT NULL, NULL, NULL, 'Testemail@gmail.com'
答案 1 :(得分:1)
INSERT INTO #TempCustomerTable
SELECT
Orders.OrderID,
Orders.OrderDate,
Orders.ShipFirstName,
Customers.EmailAddress
FROM
Orders
JOIN
Customers ON customers.customerid = orders.customerid
WHERE
Orders.OrderStatus = 'Shipped'
AND Orders.ShipDate >= DATEADD(DAY, 20, GETDATE())
AND Orders.ShipDate <= DATEADD(DAY, -5, GETDATE())
ORDER BY Orders.OrderID desc
-- If you are trying to update all users email address
UPDATE #TempCustomerTable
SET EmailAddress = 'Testemail@gmail.com'
-- If you are trying to insert new record with just email assuming no null constraints
INSERT INTO #TempCustomerTable
VALUES(NULL, NULL,NULL, 'Testemail@gmail.com')
or
-- If you are trying to update all users email address
SELECT
Orders.OrderID,
Orders.OrderDate,
Orders.ShipFirstName,
'Testemail@gmail.com' AS EmailAddress
FROM
Orders
JOIN
Customers ON customers.customerid = orders.customerid
WHERE
Orders.OrderStatus = 'Shipped'
AND Orders.ShipDate >= DATEADD(DAY, 20, GETDATE())
AND Orders.ShipDate <= DATEADD(DAY, -5, GETDATE())
ORDER BY Orders.OrderID desc