0

我将尽我所能准确地布局我正在尝试做的事情。我正在使用一个房地产数据库,该数据库有 5 个与不同类型的属性(住宅、商业、公寓、土地等)相关的表。该数据库有一个称为 MLS 的表,它将所有常见元素汇集到这些表中的一个表中。我需要查看这些表,其中主要包括来自这个常见 MLS 表的数据,但也包括来自其他几个表的一些特定数据。

我将给出一个示例,其中包括 RESIDENTIAL 和 CONDO 表中存在的字段,但不包括其他表。

在伪代码中

从 MLSTABLE、RESIDENTIALTABLE、CONDOTABLE 中选择 MLSNUMBER、(residential.CommunityAmenities 或 condo.CommunityAmenities)

这是我正在尝试的实际代码,但它给出了不明确的列名错误,因为 CommunityAmenities (和其他字段)存在于多个表中。

USE lsh_retsdata;

SELECT
    '',
    mls.mlsnum,
    mls.propertyclassid,
    '',
    mls.streetnumber,
    mls.streetname,
    '',
    mls.areaid,
    mls.streetletter,
    '',
    mls.StreetAddressDisplay,
    mls.remarks,
    mls.remarks,
    mls.city,
    mls.STATE,
    '',
    mls.zipcode,
    mls.countyid,
    "US",
    mls.latitude,
    mls.longitude,
    mls.listprice,
    '',
    '',
    mls.taxamount,
    '',
    totalbedrooms,
    totalfullbaths,
    mls.sqfttotal,
    '',
    mls.acres,
    mls.yearbuilt,
    mls.heatingsystem,
    mls.coolingsystem,
    heatingsource,
    mls.garagedescription,
    mls.garagecapacity,
    mls.zoning,
    '',
    mls.constructiontype,
    mls.roofmaterial,
    waterfrontdesc,
    CONCAT(mls.highschool,CHAR(13),mls.juniorhighschool),
    '',
    mls.style,
    associationfee,
    '',
    '',
    '',
    '',
    OfficeCoListOfficeName,
    '',
    CommunityAmenities
FROM rets_property_mls mls
LEFT JOIN rets_property_mul mul
    ON mls.mlsnum = mul.mlsnum
LEFT JOIN rets_property_auc auc
    ON mls.mlsnum = auc.mlsnum
LEFT JOIN rets_property_com com
    ON mls.mlsnum = com.mlsnum
LEFT JOIN rets_property_llf llf
    ON mls.mlsnum = llf.mlsnum
LEFT JOIN rets_property_cnd cnd
    ON mls.mlsnum = cnd.mlsnum
4

4 回答 4

2

问题是(正如您已经注意到的)MySQL 不知道CommunityAmenities您想要哪个。

您可以通过明确说明该字段应来自哪个表来解决此问题:

mul.CommunityAmenities

(将 mul 替换为相应的表)。

于 2013-01-06T21:32:40.297 回答
1

您需要使用别名来重命名列更有意义:)

SELECT t1.CommunityAmenities AS t1CommunityAmenities,
t2. CommunityAmenities AS t2CommunityAmenities
FROM t1 left join t2 on ...
于 2013-01-06T21:23:34.220 回答
1

如果您有一列可以来自多个表,但只能在一个特定表中(例如在公寓表或住宅表中但不是两者),您可以使用它 coalesce (condotable.communityamenities, residentialtable.communityamenities) 来为您提供第一个非空值。(在您的情况下,唯一的非空值)

于 2013-01-06T21:37:14.440 回答
0

In your list of selected fields, you need to quality all fields to avoid this error.

For example, instead of just CommunityAmenities in SELECT list you should use something like mls.CommunityAmenities. Otherwise, server does not know which exact table you meant - you joined many of them, and they all have this field.

于 2013-01-06T21:31:13.617 回答