我是SQL新手......
现在我想用DevExpress CxGrid和SQL显示一些AddrDistances实例。
Select Cast((DistanceAsMeters * 0.001) as Decimal(8,1)) DistanceAsKm, bold_id, created, fromAddress, toAddress
From AddrDistance
Where DistanceAsMeters = 0 and PseudoDistanceAsCostKm = 0
and not AddrDistance.bold_id in (select bold_id from DistanceQueryTask)
Order By Created Desc
这个SQL正在运行,结果是:
DistanceAsKM Bold_ID Created FromAddress ToAddress
0 134808 16.02.2010 121795 134570
0 121701 10.03.2010 120850 122991
我想要的结果是:
DistanceAsKM Bold_ID Created FromAddress ToAddress
0 134808 16.02.2010 Kalmar Stockholm
0 121701 10.03.2010 Falkenberg Oslo
所以行数是正确的,但我想用另一个表中的字符串替换FromAddress和ToAddress中的数字。这里显示的数字只是粗略的。 数据库中的每个对象都有一个唯一的粗体。 上面的地址存储在表格地址中,它有一个带有列的城市字段和一个粗体作为键。
我应该在SQL中写什么才能做到这一点? CxGrid中有什么东西可以帮助吗?
此致
EDIT1:经过一些实验,我得到了这个似乎有效的SQL:
SELECT DistanceAsMeters, Bold_Id, Created, AddressFrom.CityName_CO as FromCity, AddressTo.CityName_CO as ToCity
FROM AddrDistance
LEFT JOIN Address AddressFrom ON AddrDistance.FromAddress = AddressFrom.Bold_Id
LEFT JOIN Address AddressTo ON AddrDistance.ToAddress = AddressTo.Bold_Id
Where DistanceAsMeters = 0 and PseudoDistanceAsCostKm = 0
and not AddrDistance.bold_id in (select bold_id from DistanceQueryTask)
Order By Created Desc
非常感谢您的帮助! 彼得得到了答案,因为我认为最清楚。 顺便说一句,我使用InterBase 2009,我认为它使用SQL方言3.一个区别是它在JOIN之后不处理AS。
答案 0 :(得分:4)
您想要加入其他表格。您没有指定sql变体,但从查看语法看起来就像SQL Server / TSQL。
尝试:
Select Cast((DistanceAsMeters * 0.001) as Decimal(8,1)) DistanceAsKm, bold_id, created, AddressFrom.AddressName, AddressTo.AddressName
From AddrDistance
JOIN Address AS AddressFrom ON AddrDistance.fromAddress = AddressFrom.AddressId
JOIN Address AS AddressTo ON AddrDistance.toAddress = AddressTo.AddressId
Where DistanceAsMeters = 0 and PseudoDistanceAsCostKm = 0
and not AddrDistance.bold_id in (select bold_id from DistanceQueryTask)
Order By Created Desc
答案 1 :(得分:1)
您需要在City表上创建一个JOIN。
Select Cast((DistanceAsMeters * 0.001) as Decimal(8,1)) DistanceAsKm, bold_id, created, fromAddress, toAddress
From AddrDistance
**JOIN City c
ON ad.bold_id = c.bold_id -- Or whatever the column is**
Where DistanceAsMeters = 0 and PseudoDistanceAsCostKm = 0
and not AddrDistance.bold_id in (select bold_id from DistanceQueryTask)
Order By Created Desc
答案 2 :(得分:1)
尝试加入这些ID:
Select
Cast((DistanceAsMeters * 0.001) as Decimal(8,1)) DistanceAsKm, bold_id, created
,f.DisplayText, t.DisplayText ---<<<<<<
From AddrDistance
JOIN OtherTable f ON AddrDistance.fromAddress=f.PK ---<<<<<<
JOIN OtherTable t ON AddrDistance.toAddress=t.PK ---<<<<<<
Where DistanceAsMeters = 0 and PseudoDistanceAsCostKm = 0
and not AddrDistance.bold_id in (select bold_id from DistanceQueryTask)
Order By Created Desc