English 中文(简体)
Join 表,无Join标准
原标题:Join Tables with no Join Criteria
  • 时间:2010-09-01 23:39:01
  •  标签:
  • sql
  • tsql

这似乎非常简单,但我只能说出这一点。 我只想合并两个表格。 我不关心哪些价值观与这些价值观相配。 举例说:

declare @tbl1 table(id int)  
declare @tbl2 table(id int)  

insert @tbl1 values(1)  
insert @tbl1 values(2)  
insert @tbl2 values(3)  
insert @tbl2 values(4)  
insert @tbl2 values(5)  

select * from @tbl1, @tbl2  

This returns 6 rows, but what kind of query will generate this (just slap the tables side-by-side):
1 3
2 4
null 5

最佳回答

你们可以提供每个桌位数,然后加入:

WITH
Table1WithRowNumber as (
    select row_number() over (order by id) as RowNumber, id from Table1
),

Table2WithRowNumber as (
    select row_number() over (order by id) as RowNumber, id from Table2
)

SELECT Table1WithRowNumber.Id, Table2WithRowNumber.Id as Id2
FROM Table1WithRowNumber 
FULL OUTER JOIN Table2WithRowNumber ON Table1WithRowNumber.RowNumber = Table2WithRowNumber.RowNumber

www.un.org/chinese/ga/president

问题回答

使用十字架

  Select * From tableA Cross Join TableB

但是,大家理解,在表中所列各行的产出中,你会一行。 A with each Row in TableB...

So if Table A has 8 rows, and TableB has 4 rows, you will get 32 rows of data... If you want any less than that, you have to specify some join criteria, that will filter out the extra rows from the output

这些工作是:

Select A.ID, B.ID From
  (SELECT ROW_NUMBER () OVER (ORDER BY ID) AS RowNumber, ID FROM Tbl2 ) A
full outer join
  (SELECT ROW_NUMBER () OVER (ORDER BY ID) AS RowNumber, ID FROM Tbl1 ) B 
on (A.RowNumber=B.RowNumber)

这里也适用了LQ1交叉条目。

Select * 
From tableA, TableB




相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签