doujiu6976 2013-09-16 15:33
浏览 31
已采纳

Mysql Complex加入查询问题

I have 5 mysql tables as described below. clinics table

id
name

d_location_subscription table

id
clinic_id
t_id   //t_id will contain a foreign key of d_cities, d_states or d_countries table
type   "country" "state" "city"

d_countries table

id
name
code

d_states table

id
d_country_id
name
code

d_city table

id
d_state_id
name
code

d_location_subscription table is used to record clinic's subscription for a location(it may be a city, state or country). I'm expecting to get all subscribed cities for a specific clinic using d_location_subscription table.

For example, if clinic A is subscribed to Texas state, I should be able to get all city ids for clinic A.

I created following sql query, it looks ugly but generate a close result what i want to achieve.

select 
    `d`.`id` AS `clinic_id`,
    if((`dct`.`id` is not null),`dct`.`id`,if((`dct1`.`id` is not null),`dct1`.`id`,`dct2`.`id`)) AS `d_city_id` 
from ((((((((
    `d_location_subscriptions` `dls` 
    join `clinics` `d` 
        on((`d`.`id` = `dls`.`clinic_id`))) 
    left join `d_countries` `dc` 
        on(((`dc`.`id` = `dls`.`t_id`) and (`dls`.`type` = 'country')))) 
    left join `d_states` `ds` 
        on((`ds`.`d_country_id` = `dc`.`id`))) 
    left join `d_cities` `dct2` 
        on((`dct2`.`d_state_id` = `ds`.`id`))) 
    left join `d_states` `ds1` 
        on(((`ds1`.`id` = `dls`.`t_id`) and (`dls`.`type` = 'state')))) 
    left join `d_cities` `dct` 
        on((`dct`.`d_state_id` = `ds1`.`id`))) 
    left join `d_cities` `dct1` 
        on(((`dct1`.`id` = `dls`.`t_id`) and (`dls`.`type` = 'city')))) 
) 

when there is record with type "country" in d_location_subscription table, I receive following result. total number of records returned are equal to the number of d_states table records.

this is the result

How should I get rid of those Null values by changing above query? And please advice me if this is the correct way to acheive similar functionality. Thanks in advance :)

  • 写回答

2条回答 默认 最新

  • dsg56465 2013-09-16 16:01
    关注

    the IF() computed column is in essence what STT LCU was trying to offer, but you can't use that directly in the where for some reason.

    I've rewritten your query, but with different aliases to better follow the origination of the tables / relationships to get the data. In the end, I've added a where to test for ANY ONE of the "ID" values as NOT NULL. If they are ALL Null, the record should be excluded..

    select 
          d.id AS clinic_id,
          if(CityViaState.id is not null, CityViaState.id,
             if( ByCity.id is not null, ByCity.id, CityViaCountry.id )) AS d_city_id 
       from 
          d_location_subscriptions dls 
             join clinics d 
                ON dls.clinic_id = d.id 
    
             left join d_countries ByCountry 
                ON dls.t_id = ByCountry.id 
                and dls.type = 'country'
                left join d_states StateViaCountry 
                   ON ByCountry.id = StateViaCountry.d_country_id 
                   left join d_cities CityViaCountry 
                      ON StateViaCountry.id = CityViaCountry.d_state_id 
    
             left join d_states ByState 
                ON dls.t_id = ByState.id 
                and dls.type = 'state'
                left join d_cities CityViaState 
                   ON ByState.id = CityViaState.d_state_id
    
             left join d_cities ByCity 
                ON dls.t_id = ByCity.id 
                and dls.type = 'city'
       where
             CityViaState.id is not null
          OR ByCity.id is not null
          OR CityViaCountry.id is not null
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上