如何完成复杂的左联接?

时间:2019-03-16 08:23:14

标签: mysql join

我最近一直在一个项目中,将数据从MLS列表转换为MySQL数据库。目前,我一直试图将数据连接到一个表中,以简化从Microsoft BI的查询。

        create table listing_fact as 
select 
  coalesce(ld.ListingID, 0) ListingID
,  coalesce(Bd.BathroomID, 0) BathroomID
,  coalesce(Hd.HVACID, 0) HVACID
, coalesce(gd.GarageID, 0) GarageID
, coalesce(sd.SchoolID, 0) SchoolID
, coalesce(pid.PublicInfoID, 0) PublicInfoID
, mls_stage.apx_total_sqft
from mls_stage m
left join
  listing_dim ld
    on m.mls_number = ld.mls_number
left join
  hvac_dim hd
 on case    when m.heat_source_type like '%propane%' then 'Propane' 
    when m.heat_source_type like '%gas%' then 'Gas'
    when m.heat_source_type like '%electric%' then 'Electric'
    when m.heat_source_type like '%Heat Pump%' then 'Heat Pump'
    when m.heat_source_type like '%oil%' then 'Oil'
    when m.heat_source_type like '%none%' then 'None'
    else null
end = hd.heat_source and
case 
    when m.heat_source_type like '%forced air%' then 'Forced Air'
    when m.heat_source_type like '%baseboard%' then 'Baseboard'
    when m.heat_source_type like '%Radiant%' then 'Radiant'
    when m.heat_source_type like '%zonal%' then 'Zonal'
    when m.heat_source_type like '%none%' then 'None'
    else null
end = hd.Heat_Type and
case 
    when m.air_conditioning like '%Central%' then 'Central'
    when m.air_conditioning like '%None%' then 'None'
    when m.air_conditioning like '%Evaporative Cooler%' then 'Evaporative Cooler'
    when m.air_conditioning like '%other%' then 'Other'
    else null
    end = hd.air_conditioning
left join
   garage_dim gd
     on case
  when m.garage_num_stalls_type like '%1%'
    then 1
  when m.garage_num_stalls_type like '%2%'
    then 2
  when m.garage_num_stalls_type like '%3%'
    then 3
  when m.garage_num_stalls_type like '%4%'
    then 4
  when m.garage_num_stalls_type like '%5%'
    then 5
  when m.garage_num_stalls_type like '%6%'
    then 6
  when m.garage_num_stalls_type like '%none%'
    then 0
    else null
  end = gd.Number_of_Stalls
and CASE
    WHEN m.garage_num_stalls_type LIKE '%Attached%' then 'Attached'
    when m.garage_num_stalls_type like '%detached%' then 'Detached'
    when m.garage_num_stalls_type like '%Carport%' then 'Carport'
    when m.garage_num_stalls_type like '%None%' or '%N/A%' then 'None'
    else null
end = gd.Garage_Style
left join
  bathroom_dim bd
on m.bathrooms = bd.total_bathrooms and
m.upper_num_3_4_baths and
m.main_num_3_4_baths and 
m.lower_num_3_4_baths and
m.basement_num_3_4_baths and 
m.upper_num_3_4_baths + m.main_num_3_4_baths + m.lower_num_3_4_baths + m.basement_num_3_4_baths = bd.Total_3_4_baths and
m.total_half_baths
left join
  school_dim sd
    on m.elementary_school = sd.elementary_school
     and mls_stage.middle_school = sd.middle_school
   and mls_stage.high_school = sd.high_school
where mls_number < 1;

每当我尝试运行此代码时,都会出现以下错误:

Error Code: 1049. Unknown database 'm'

我期望m是MLS_Stage的表。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

也许您在查询之前没有选择数据库。
使用此命令选择您想要的数据库:

use [your database name]