1

我试图首先从“GermanDB”数据库中打印出结果,同时还显示波士顿数据库中不在德国数据库中的所有内容。这可以在一个查询中完成吗?

我的查询(粗体部分起作用,但没有按我想要的方式排序)

select * from (  
SELECT DISTINCT a.ProductRef  
FROM GERMANDB.dbo.LOCATIONS AS a INNER JOIN GERMANDB.dbo.ITEMS AS b ON a.ProductRef =   b.ProductRef   
WHERE b.ACTIVE=1  
) ta  
UNION select * from  
SELECT DISTINCT c.ProductRef  
FROM BOSTONDB.dbo.LOCATIONS AS c INNER JOIN BOSTONDB.dbo.ITEMS AS d ON c.ProductRef =   d.ProductRef   
WHERE c.ACTIVE=1 (c.ProductRef NOT IN   
(SELECT ProductRef FROM GERMANDB.dbo.ITEMS where ACTIVE=1))  
) tb  
order by ta.ProductRef** , tb.productRef
4

1 回答 1

13

只需添加一个字段来表示优先级。像这样:

select *, 0 as Priority from (  
SELECT DISTINCT a.ProductRef  
FROM GERMANDB.dbo.LOCATIONS AS a INNER JOIN GERMANDB.dbo.ITEMS AS b ON a.ProductRef =   b.ProductRef   
WHERE b.ACTIVE=1  
) ta  
UNION select *, 1 as Priority from  
SELECT DISTINCT c.ProductRef  
FROM BOSTONDB.dbo.LOCATIONS AS c INNER JOIN BOSTONDB.dbo.ITEMS AS d ON c.ProductRef =   d.ProductRef   
WHERE c.ACTIVE=1 (c.ProductRef NOT IN   
(SELECT ProductRef FROM GERMANDB.dbo.ITEMS where ACTIVE=1))  
) tb  
order by Priority, ProductRef
于 2013-10-02T20:36:15.550 回答