为什么这是错的,我该怎么说呢?
SELECT PublicationID
FROM (SELECT DISTINCT pat.PublicationID
FROM dbo.PubAdvTransData AS pat INNER JOIN
dbo.PubAdvertiser AS pa ON pat.AdvTransID = pa.AdvTransID
WHERE (pat.LastAdDate > '7 / 1 / 2009') AND (pat.PublicationID = 29171)) AS table1
UNION
SELECT PublicationAreaBuy.AreaBuyID AS PublicationID
FROM PublicationAreaBuy INNER JOIN
table1 AS table1_1 ON table1.publicationID = PublicationAreaBuy.PublicationID
错误是table1是无效对象。
感谢。
答案 0 :(得分:7)
您有一个范围问题。联合组合了两个单独的查询。因此,如果您将查询分开:
SELECT PublicationID
FROM ( SELECT DISTINCT pat.PublicationID
FROM dbo.PubAdvTransData AS pat
INNER JOIN dbo.PubAdvertiser AS pa
ON pat.AdvTransID = pa.AdvTransID
WHERE (pat.LastAdDate > '7 / 1 / 2009') AND (pat.PublicationID = 29171)
) AS table1
SELECT PublicationAreaBuy.AreaBuyID AS PublicationID
FROM PublicationAreaBuy
INNER JOIN table1 AS table1_1
ON table1.publicationID = PublicationAreaBuy.PublicationID
你可以看到在第二个查询中没有table1这样的东西。还有其他一些方法可以实现您的目标:
如果我们要复制子查询,它看起来像这样。但是,顶部查询没有条件,因此它不需要是子查询:
SELECT DISTINCT pat.PublicationID
FROM dbo.PubAdvTransData AS pat
INNER JOIN dbo.PubAdvertiser AS pa
ON pat.AdvTransID = pa.AdvTransID
WHERE (pat.LastAdDate > '7 / 1 / 2009') AND (pat.PublicationID = 29171)
UNION
SELECT PublicationAreaBuy.AreaBuyID AS PublicationID
FROM PublicationAreaBuy
INNER JOIN ( SELECT DISTINCT pat.PublicationID
FROM dbo.PubAdvTransData AS pat
INNER JOIN dbo.PubAdvertiser AS pa
ON pat.AdvTransID = pa.AdvTransID
WHERE (pat.LastAdDate > '7 / 1 / 2009') AND (pat.PublicationID = 29171)
) AS table1
ON table1.publicationID = PublicationAreaBuy.PublicationID
答案 1 :(得分:1)
嗯..是的。
两个select语句彼此不同,即使结果由union联合在一起。
您使用的是什么SQL服务器,包括版本?
答案 2 :(得分:1)
AFAIK,你不能引用另一个查询。你要联合在一起的两个查询必须是分开的和不同的。
当你考虑这个时,它是有道理的,因为所有联盟真正做的是将两个查询的输出连续拼接在一起。
答案 3 :(得分:1)
你可以使用临时表,但我觉得可能有一个更好的方法。在不知道您的数据库架构或您想要实现的目标的情况下,很难推荐其他任何内容。
CREATE TABLE #temp
(
PublicationID int
)
SELECT DISTINCT INTO #temp pat.PublicationID
FROM dbo.PubAdvTransData AS pat
INNER JOIN dbo.PubAdvertiser AS pa ON pat.AdvTransID = pa.AdvTransID
WHERE (pat.LastAdDate > '7 / 1 / 2009') AND (pat.PublicationID = 29171)
SELECT PublicationID
FROM #temp
UNION
SELECT PublicationAreaBuy.AreaBuyID AS PublicationID
FROM PublicationAreaBuy
INNER JOIN #temp As Table1 ON Table1.PublicationID = PublicationAreaBuy.PublicationID
免责声明:我手动输入,请在运行前检查语法错误。
答案 4 :(得分:0)
尝试使用CTE代替您的第一个查询
WITH MyCTE AS
(
SELECT DISTINCT pat.PublicationID
FROM dbo.PubAdvTransData AS pat
INNER JOIN dbo.PubAdvertiser AS pa ON pat.AdvTransID = pa.AdvTransID
WHERE (pat.LastAdDate > '7 / 1 / 2009') AND (pat.PublicationID = 29171)
)
SELECT PublicationAreaBuy.AreaBuyID AS PublicationID
FROM PublicationAreaBuy
INNER JOIN MyCTE ON MyCTE.publicationID = PublicationAreaBuy.PublicationID