English 中文(简体)
SQL 服务器连接不相关的表格[已关闭]
原标题:SQL server join unrelated tables [closed]

我有两张没有压抑的桌子:

人口表:

   ID | NAME | AGE
    ------------
   1  | DAVE | 43
   1  | ANNE | 25
   1  | MIKE | 58

PLACES 表格 :

ID | COUNTRY | TOWN
------------------
1  |   USA    | WILMINGTON
1  |   GER    | BERLIN
1  |   POR    | LISBON
1  |   JPN    | KYOTO

我想要这样的结果:

NAME | AGE | COUNTRY | TOWN
-----------------------------
ANNE |  25 | GER     | BERLIN
DAVE |  43 | JPN     | KYOTO
MIKE |  58 | POR     | LISBON
           | USA     | WILMINGTON
问题回答

让我试试看

SELECT COALESCE(a.Name,   ) Name,
       COALESCE(a.Age,   ) Age,
       b.Country,
       b.Town
FROM
    (SELECT ROW_NUMBER() OVER (ORDER BY [Name]) AS RowNum, Name, Age
     FROM   People) a Right Join 
    (SELECT ROW_NUMBER() OVER (ORDER BY [Country]) AS RowNum, Country, Town
     FROM   Places) b ON a.RowNum = b.RowNum

您需要在每个表格中至少一张数据才能加入对应。 通常, 此选项会处理初级密钥/ 外国密钥关系 。 您可以在您的 PLACES 表格中创建一个主密钥( 如 1=USA, 2=GER, 3=POR, 4=JPN), 然后将外国密钥放在您人口表格中适合每个人的位置的一列( 如 2=ANNE, 4=DAVE, 3=MIKE) 。

我猜你希望按字母顺序把这些组合起来 但是这不怎么合理...

WITH x AS (SELECT *, rn = ROW_NUMBER() OVER (ORDER BY NAME) FROM People),
     y AS (SELECT *, rn = ROW_NUMBER() OVER (ORDER BY COUNTRY) FROM Places)
SELECT * FROM x FULL OUTER JOIN y ON x.rn = y.rn;

我完全不建议这样做! 但我觉得这是一个很好的挑战。我强烈建议采用Sean的建议,用钥匙来这样做。

无论如何,这个查询会准确提供你所要求的内容,而不改变你的计划(它也假设你的数据是准确的表示)。

示例:http://www.sqlfiddle.com/#!3/a0486/8

Select Distinct
  case when town =  Wilmington  Then NULL else Name end as Name, 
  case when town =  Wilmington  Then NULL else Age end as Age,
  Country, 
  Town
FROM People, Places
Where 
     (Name =  Anne  AND Town =  Berlin )
  OR (Name =  Dave  AND Town =  Kyoto )
  OR (Name =  Mike  AND Town =  Lisbon )
  OR (Town =  Wilmington )
Order By Country Asc

<强 > EDIT

为了完成我的回答并提供一个更合适的解决办法,我建议你在两个表格中加上一个PlaceID,然后左翼加入其中。

示例:http://www.sqlfiddle.com/#!3/d5ee8/3

Create Table People(
  Name varchar(255),
  Age int,
  PlaceID int
)

Create Table Places(
  PlaceID int,
  Country varchar(255),
  Town varchar(255)
)

Insert Into People Values ( Dave , 43, 2)
Insert Into People Values ( Anne , 25, 1)
Insert Into People Values ( Mike , 58, 3)

Insert Into Places Values (1,  Ger ,  Berlin )
Insert Into Places Values (2,  Jpn ,  Kyoto )
Insert Into Places Values (3,  Por ,  Lisbon )
Insert Into Places Values (4,  USA ,  Wilmington )

Select 
  Name, Age, Country, Town 
FROM 
  Places
  Left Join People On Places.PlaceID = People.PlaceID




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签