0

I have codes table with following data

_________
dbo.Codes
_________
AXV
VHT
VTY

and email table with the folowing data

_________
dbo.email
_________
x@gmail.com
y@gmail.com
z@gmail.com

and I am looking forward to join these two tables horizontally with the following output.

__________
dbo.output
__________ 
AXV    x@gmail.com
VHT    y@gmail.com
VTY    z@gmail.com

Is there any way possible to get the desired output?

Edit #1 Both the tables contain unique codes and unique email addresses

4

4 回答 4

2

假设这是 SQLServer,请尝试:

; with 
c as (select codes, row_number() over (order by codes) r from codes),
e as (select email, row_number() over (order by email) r from email)
select codes, email
from c join e on c.r = e.r
order by c.r
于 2013-05-24T11:33:29.760 回答
1

你可以这样做。

select c.Codes , e.email
from CodesTable c, emailTable e
where c.rownum = e.rownum;
于 2013-05-24T11:34:14.103 回答
0

创建一个新表

创建表测试(代码varchar(32),电子邮件varchar(32)唯一(代码),唯一(电子邮件));

执行这条sql的结果

选择'插入测试(代码,电子邮件)值('''+代码+''','''+电子邮件+''');' 从 test1 , test2

所有的sql都不会成功执行,但是表会按顺序填满
当你选择测试表时,你会得到你想要的

于 2013-05-24T11:47:23.033 回答
0
SELECT email, code
FROM 
(SELECT email, ROW_NUMBER() OVER(ORDER BY email) AS RowNum FROM email) AS T1
OUTER JOIN 
(SELECT code, ROW_NUMBER() OVER(ORDER BY code) AS RowNum FROM codes) AS T2
ON T2.RowNum = T1.RowNum
ORDER BY COALESCE(T1.RowNum, T2.RowNum)
于 2013-05-24T11:43:16.060 回答