我完全不推荐这个!!!但我发现这是一个很好的挑战。我强烈建议您按照 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
编辑
为了完成我的回答并提供更合适的解决方案,我建议您将 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