我完全不建议这样做! 但我觉得这是一个很好的挑战。我强烈建议采用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