链接两个表没有任何共同点

时间:2012-10-11 08:40:44

标签: sql-server-2008 join

我正在尝试加入这些陈述:

select min(len(Password)) as min, 
       max(len(Password)) as max, 
       avg(len(Password)) as avg 
FROM Customer.dbo.Password 

UNION ALL
select min(len(password2)) as min, 
       max(len(password2)) as max, 
       avg(len(password2)) as avg 
FROM website.dbo.password2

对于这些:

SELECT c.name COLLATE SQL_Latin1_General_CP1_CI_AS colName, o.name COLLATE SQL_Latin1_General_CP1_CI_AS tableName, 'website' db FROM website.sys.all_columns as c INNER JOIN website.sys.all_objects o ON c.object_id = o.object_id WHERE c.name like '%password%' AND type = 'U'

UNION
SELECT c.name COLLATE SQL_Latin1_General_CP1_CI_AS colName, o.name COLLATE SQL_Latin1_General_CP1_CI_AS tableName, 'Customer' db FROM Customer.sys.all_columns as c INNER JOIN Customer.sys.all_objects o ON c.object_id = o.object_id WHERE c.name like '%password%' AND type = 'U' 

他们没有共同的列。前两个SELECT语句生成三列min,max和avg密码长度,后两个生成密码所在的colName,tableName和db名称。

我想纯粹出于视觉原因在一个表中显示两个结果集,以获得相应的minName,max,avg到相应的colName,tableName和db。

我试过这样做:

DECLARE @numberTable TABLE (link nvarchar(500), max int, min int, avg int)
INSERT INTO @numberTable

select 'link', min(len(Password)) as min, 
       max(len(Password)) as max, 
       avg(len(Password)) as avg 
FROM Customer.dbo.Password UNION ALL
select 'link', min(len(password2)) as min, 
       max(len(password2)) as max, 
       avg(len(password2)) as avg 
FROM website.dbo.password2

SELECT * FROM @numberTable ORDER BY max

DECLARE @myTable TABLE (link nvarchar(500), colName nvarchar(500), tableName   nvarchar(500), db nvarchar(500))
INSERT INTO @myTable

SELECT 'link', c.name COLLATE SQL_Latin1_General_CP1_CI_AS colName, o.name COLLATE SQL_Latin1_General_CP1_CI_AS tableName, 'website' db FROM website.sys.all_columns as c INNER JOIN website.sys.all_objects o ON c.object_id = o.object_id WHERE c.name like '%password%' AND type = 'U' UNION

SELECT 'link', c.name COLLATE SQL_Latin1_General_CP1_CI_AS colName, o.name COLLATE SQL_Latin1_General_CP1_CI_AS tableName, 'Customer' db FROM Customer.sys.all_columns as c INNER JOIN Customer.sys.all_objects o ON c.object_id = o.object_id WHERE c.name like '%password%' AND type = 'U' 

SELECT * FROM @myTable ORDER BY db

SELECT link
FROM @myTable
LEFT OUTER JOIN @numberTable
ON @myTable.link = @numberTable.link

不幸的是,这不起作用。

我正在使用Microsoft SQL Server 2008。

希望我所追求的是有道理的。

2 个答案:

答案 0 :(得分:1)

您可以使用cross join加入两个不相关的表:

SELECT  c.name COLLATE SQL_Latin1_General_CP1_CI_AS colName
,       o.name COLLATE SQL_Latin1_General_CP1_CI_AS tableName
,       'website' db 
,       pwd.[min]
,       pwd.[max]
,       pwd.[avg]
FROM    website.sys.all_columns as c 
INNER JOIN 
        website.sys.all_objects o 
ON      c.object_id = o.object_id 
CROSS JOIN
        (
        select min(len(Password)) as min, 
               max(len(Password)) as max, 
               avg(len(Password)) as avg 
        FROM   website.dbo.Password        
        ) as pwd
WHERE   c.name like '%password%' 
        AND type = 'U' 

UNION

SELECT  c.name COLLATE SQL_Latin1_General_CP1_CI_AS colName
,       o.name COLLATE SQL_Latin1_General_CP1_CI_AS tableName
,       'Customer' db 
,       pwd.[min]
,       pwd.[max]
,       pwd.[avg]
FROM    Customer.sys.all_columns as c 
INNER JOIN 
        Customer.sys.all_objects o 
ON      c.object_id = o.object_id 
CROSS JOIN
        (
        select min(len(Password)) as min, 
               max(len(Password)) as max, 
               avg(len(Password)) as avg 
        FROM    Customer.dbo.Password        
        ) as pwd
WHERE   c.name like '%password%' 
        AND type = 'U'

答案 1 :(得分:0)

创建他们共同拥有的任意内容并加入

Select * 
from
(
select 
       1 as RowID,
       min(len(Password)) as min,  
       max(len(Password)) as max,  
       avg(len(Password)) as avg  
FROM Customer.dbo.Password  

UNION ALL 
select 
       2,
       min(len(password2)) as min,  
       max(len(password2)) as max,  
       avg(len(password2)) as avg  
FROM website.dbo.password2 
) t1
inner join
(


SELECT 1 as RowID, c.name COLLATE SQL_Latin1_General_CP1_CI_AS colName, o.name COLLATE SQL_Latin1_General_CP1_CI_AS tableName, 'website' db FROM website.sys.all_columns as c INNER JOIN website.sys.all_objects o ON c.object_id = o.object_id WHERE c.name like '%password%' AND type = 'U' 

UNION 
SELECT 2, c.name COLLATE SQL_Latin1_General_CP1_CI_AS colName, o.name COLLATE SQL_Latin1_General_CP1_CI_AS tableName, 'Customer' db FROM Customer.sys.all_columns as c INNER JOIN Customer.sys.all_objects o ON c.object_id = o.object_id WHERE c.name like '%password%' AND type = 'U' 
) t2
    on t1.rowID = t2.rowID