아래에 설명 된대로 5 개의 mysql 테이블이 있습니다. 클리닉 테이블
id
name
d_location_subscription 테이블
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 테이블
id
name
code
d_states 테이블
id
d_country_id
name
code
d_city 테이블
id
d_state_id
name
code
d_location_subscription 테이블은 위치 (도시, 주 또는 국가 일 수 있음)에 대한 클리닉의 구독을 기록하는 데 사용됩니다. d_location_subscription 테이블을 사용하여 특정 클리닉에 대해 모든 구독 도시를 가져올 것으로 예상합니다.
예를 들어 클리닉 A가 텍사스 주에 가입되어 있다면 클리닉 A의 모든 도시 ID를 얻을 수 있습니다.
나는 다음과 같은 SQL 쿼리를 만들었는데, 추악 해 보이지만 내가 원하는 것을 가까운 결과를 생성합니다.
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'))))
)
d_location_subscription 테이블에 "country"유형의 레코드가있을 때 다음과 같은 결과를받습니다. 반환 된 총 레코드 수는 d_states 테이블 레코드 수와 같습니다.
위의 쿼리를 변경하여 Null 값을 어떻게 제거해야합니까? 그리고 이것이 유사한 기능을 얻는 올바른 방법인지 저에게 조언하십시오. 미리 감사드립니다 :)
IF () 계산 열은 본질적으로 STT LCU가 제공하려고했던 것입니다. 그러나 어떤 이유로 든 직접 사용할 수는 없습니다.
쿼리를 다시 작성했지만 데이터를 얻기 위해 테이블 / 관계의 기원을 더 잘 따르기 위해 다른 별칭을 사용했습니다. 결국, "ID"값 중 하나를 NOT NULL로 테스트 할 위치를 추가했습니다. ALL Null이면 레코드를 제외해야합니다.
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
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다