2011-12-08 4 views
8

私はこれを行うと:MySQLビューはANDでさらに多くの結果を返しますか?

select * from vw_active_employees where division IS NULL; --319 results 

は理にかなっています。そして、私はこれを行う...

select * from vw_active_employees where division IS NULL AND udds IS NULL; -- over 1000 results 

Whaaaaat?より多くの結果??

私は、それが私が期待していたものを返すために、ビューをサブセレクトとしてラップする必要がありました。このように:

select vw_active_employees.* from (select * from vw_active_employees) tmp where division IS NULL AND udds IS NULL; --317 results 

誰かがこれを説明できますか?私はMS SQLSERVERでこれを行う必要はありませんでした - これは私にとっては外国です。



このビューを作成するために使用されるSQLは、実際に一緒に他のビューの数を結合します。私はそれを見るために便利です全くわからないんだけど、それはそれは基本的に

CREATE VIEW `vw_active_employees` AS 

select 
e.*, 
`vw_employee_attributes_map`.`med_school_faculty`, `vw_employee_attributes_map`.`paid_clinic_faculty`, `vw_employee_attributes_map`.`volunteer_clinic_faculty`, `vw_employee_attributes_map`.`dept_vote_rights`, `vw_employee_attributes_map`.`emeritus`, `vw_employee_attributes_map`.`aoa_member`, `vw_employee_attributes_map`.`faculty_senate`, `vw_employee_attributes_map`.`faculty_senator_elect`, `vw_employee_attributes_map`.`faculty_senator_alt_elect`, `vw_employee_attributes_map`.`exec_comm_member`, `vw_employee_attributes_map`.`ucc`, `vw_employee_attributes_map`.`icc`, `vw_employee_attributes_map`.`va`, `vw_employee_attributes_map`.`uwmf`, `vw_employee_attributes_map`.`affiliate`, `vw_employee_attributes_map`.`aurora`, `vw_employee_attributes_map`.`website_searchable`, 
`vw_employee_current_appointment_info`.`termination_date`, `vw_employee_current_appointment_info`.`last_promotion_date`, `vw_employee_current_appointment_info`.`promotion_due_date`, `vw_employee_current_appointment_info`.`last_reappointment_date`, `vw_employee_current_appointment_info`.`reappointment_duration`, `vw_employee_current_appointment_info`.`reappointment_due_date`, `vw_employee_current_appointment_info`.`non_renewal_date`, `vw_employee_current_appointment_info`.`roster`, `vw_employee_current_appointment_info`.`payroll`, `vw_employee_current_appointment_info`.`on_probation`, `vw_employee_current_appointment_info`.`probation_complete`, `vw_employee_current_appointment_info`.`probation_notify_sent_date`, `vw_employee_current_appointment_info`.`probation_end_date`, `vw_employee_current_appointment_info`.`uw_appointment_id`, `vw_employee_current_appointment_info`.`effective_date`, `vw_employee_current_appointment_info`.`percent`, `vw_employee_current_appointment_info`.`end_date`, `vw_employee_current_appointment_info`.`continuity_status`, `vw_employee_current_appointment_info`.`guaranteed_length`, `vw_employee_current_appointment_info`.`end_reason`, `vw_employee_current_appointment_info`.`midterm_eval_received`, `vw_employee_current_appointment_info`.`final_eval_received`, `vw_employee_current_appointment_info`.`annual_eval_letter_sent`, `vw_employee_current_appointment_info`.`annual_eval_sent_date`, `vw_employee_current_appointment_info`.`annual_eval_received`, `vw_employee_current_appointment_info`.`annual_eval_received_date`, `vw_employee_current_appointment_info`.`probation_month_done`, `vw_employee_current_appointment_info`.`seniority_date`, `vw_employee_current_appointment_info`.`represented`, `vw_employee_current_appointment_info`.`work_schedule`, `vw_employee_current_appointment_info`.`evaluation_end_date`, `vw_employee_current_appointment_info`.`evaluation_month_end`, `vw_employee_current_appointment_info`.`evaluation_sent_date`, `vw_employee_current_appointment_info`.`evaluation_completed`, `vw_employee_current_appointment_info`.`hiring_pi`, `vw_employee_current_appointment_info`.`appointment_type`, `vw_employee_current_appointment_info`.`appointment_type_code`, `vw_employee_current_appointment_info`.`appointment_classified`, `vw_employee_current_appointment_info`.`termination_type`, `vw_employee_current_appointment_info`.`head`, `vw_employee_current_appointment_info`.`secretary`, `vw_employee_current_appointment_info`.`division`, `vw_employee_current_appointment_info`.`supervisor_first_name`, `vw_employee_current_appointment_info`.`supervisor_last_name`, `vw_employee_current_appointment_info`.`title`, `vw_employee_current_appointment_info`.`title_code`, `vw_employee_current_appointment_info`.`udds`, `vw_employee_current_appointment_info`.`udds_code`, 
`vw_employee_current_background_info`.`visa_holder`, `vw_employee_current_background_info`.`visa_type`, `vw_employee_current_background_info`.`visa_expiration_date`, `vw_employee_current_background_info`.`license`, `vw_employee_current_background_info`.`license_exp_date`, `vw_employee_current_background_info`.`dea`, `vw_employee_current_background_info`.`dea_expiration_date`, `vw_employee_current_background_info`.`national_provider_number`, `vw_employee_current_background_info`.`residency_location`, `vw_employee_current_background_info`.`residency_end_date`, `vw_employee_current_background_info`.`fellowship_location`, `vw_employee_current_background_info`.`fellowship_end_date`, `vw_employee_current_background_info`.`primary_board_cert`, `vw_employee_current_background_info`.`primary_cert_date`, `vw_employee_current_background_info`.`specialty_board_cert`, `vw_employee_current_background_info`.`specialty_cert_date`, `vw_employee_current_background_info`.`degree_info_reports`, `vw_employee_current_background_info`.`healthlink_id`, `vw_employee_current_background_info`.`uwmf_general_ledger_id`, `vw_employee_current_background_info`.`uwmf_employee_id`, `vw_employee_current_background_info`.`specialty`, 
`vw_employee_current_compliance_info`.`hipaa_training_completed`, `vw_employee_current_compliance_info`.`sic_training_completed`, `vw_employee_current_compliance_info`.`background_check_completed`, `vw_employee_current_compliance_info`.`i9_completed`, `vw_employee_current_compliance_info`.`caregiver_applies`, `vw_employee_current_compliance_info`.`caregiver_check_completed`, `vw_employee_current_compliance_info`.`tb_completed`, `vw_employee_current_compliance_info`.`rubella_immunity_comfirmed`, `vw_employee_current_compliance_info`.`respiratory_test_completed`, `vw_employee_current_compliance_info`.`health_link`, `vw_employee_current_compliance_info`.`access_request_uwhc`, `vw_employee_current_compliance_info`.`rn_credentialing`, `vw_employee_current_compliance_info`.`bls_cert_expiration`, `vw_employee_current_compliance_info`.`uwhc_cred_submitted`, 
`vw_employee_current_contact_info`.`employee_id`, `vw_employee_current_contact_info`.`ext_office`, `vw_employee_current_contact_info`.`area_code_office`, `vw_employee_current_contact_info`.`number_office`, `vw_employee_current_contact_info`.`country_code_office`, `vw_employee_current_contact_info`.`ext_home`, `vw_employee_current_contact_info`.`area_code_home`, `vw_employee_current_contact_info`.`number_home`, `vw_employee_current_contact_info`.`country_code_home`, `vw_employee_current_contact_info`.`address1_office`, `vw_employee_current_contact_info`.`address2_office`, `vw_employee_current_contact_info`.`city_office`, `vw_employee_current_contact_info`.`state_office`, `vw_employee_current_contact_info`.`zip_office`, `vw_employee_current_contact_info`.`mail_code_office`, `vw_employee_current_contact_info`.`address1_home`, `vw_employee_current_contact_info`.`address2_home`, `vw_employee_current_contact_info`.`city_home`, `vw_employee_current_contact_info`.`state_home`, `vw_employee_current_contact_info`.`zip_home`, `vw_employee_current_contact_info`.`email` 

from employee e 
left join vw_employee_attributes_map on e.id = vw_employee_attributes_map.employee_id 
left join vw_employee_current_appointment_info on e.id = vw_employee_current_appointment_info.employee_id 
left join vw_employee_current_background_info on e.id = vw_employee_current_background_info.employee_id 
left join vw_employee_current_compliance_info on e.id = vw_employee_current_compliance_info.employee_id 
left join vw_employee_current_contact_info on e.id = vw_employee_current_contact_info.employee_id 
where e.active = 1 

など「部門」と「UDDS」ルックス含まvw_employee_current_appointment_infoだ:)のためにあなたは尋ねた:

CREATE VIEW `vw_employee_current_appointment_info` AS 

select e.id as `employee_id`, 

`appointment`.`termination_date`, `appointment`.`last_promotion_date`, `appointment`.`promotion_due_date`, `appointment`.`last_reappointment_date`, `appointment`.`reappointment_duration`, `appointment`.`reappointment_due_date`, `appointment`.`non_renewal_date`, `appointment`.`roster`, `appointment`.`payroll`, `appointment`.`on_probation`, `appointment`.`probation_complete`, `appointment`.`probation_notify_sent_date`, `appointment`.`probation_end_date`, `appointment`.`uw_appointment_id`, `appointment`.`effective_date`, `appointment`.`percent`, `appointment`.`end_date`, `appointment`.`continuity_status`, `appointment`.`guaranteed_length`, `appointment`.`end_reason`, `appointment`.`midterm_eval_received`, `appointment`.`final_eval_received`, `appointment`.`annual_eval_letter_sent`, `appointment`.`annual_eval_sent_date`, `appointment`.`annual_eval_received`, `appointment`.`annual_eval_received_date`, `appointment`.`probation_month_done`, `appointment`.`seniority_date`, `appointment`.`represented`, `appointment`.`work_schedule`, `appointment`.`evaluation_end_date`, `appointment`.`evaluation_month_end`, `appointment`.`evaluation_sent_date`, `appointment`.`evaluation_completed`, `appointment`.`hiring_pi`, 
`appointment_type`.`name` as `appointment_type`, `appointment_type`.`code` as `appointment_type_code`, `appointment_type`.`classified` as `appointment_classified`, 
`termination`.`name` as `termination_type`, 
`appointment_division`.`head`, `appointment_division`.`secretary`, 
`division`.`division_name` as `division`, 
`supervisor`.`first_name` as `supervisor_first_name`, `supervisor`.`last_name` as `supervisor_last_name`, 
`title`.`name` as `title`, `title`.`code` as `title_code`, 
`udds`.`name` as `udds`, `udds`.`code` as `udds_code` 

from employee e 
left join appointment on appointment.employee_id = e.id and appointment.primary = 1 
left join appointment_type on appointment.appointment_type_id = appointment_type.id 
left join termination on appointment.termination_id = termination.id 
left join appointment_division on appointment_division.appointment_id = appointment.id and appointment_division.primary = 1 
left join division on appointment_division.division_id = division.id 
left join appointment_supervisor on appointment_supervisor.appointment_id = appointment.id and appointment_supervisor.primary = 1 
left join employee supervisor on appointment_supervisor.supervisor_id = supervisor.id 
left join appointment_title on appointment_title.appointment_id = appointment.id and appointment_title.primary = 1 
left join title on appointment_title.title_id = title.id 
left join appointment_udds on appointment_udds.appointment_id = appointment.id and appointment_udds.primary = 1 
left join udds on udds.id = appointment_udds.udds_id 
+4

ビューを表示してください(どのビューを使用してビューvw_active_employeesを作成しましたか?) – cristian

+0

ビューに上記のビューcreate文を追加しました。 'uddsはnull'のように思えますが、何とかして下に垂れ下がり、基になるビューSQLに影響します。これは私には間違っています。私の見解を理解するには、結果が設定されたブラックボックスにする必要があります。また、適用する基準は、ビューのデータセットを作成する基礎となるクエリに影響してはなりません。 – bmherold

+0

マニュアルでは "IS"の方が "AND"より優先順位が高いですが、試してみました: '(除算IS NULL)AND(udds IS NULL); ' –

答えて

-1

あなたを左結合を使用しているため、値が存在しない場合でも、下位のテーブルに下位のテーブルが接続されます。ただし、後続の表をフィルタリングするときは、後続の表に特定の値が含まれていることを指定しています。この場合、「vw_employee_current_appointment_info」には319の値のみが存在し、分割フィールドにはnullが含まれている可能性があります。 uddsフィールドに追加すると、分割フィールドがNULLで定義されているので、左側の結合のためにnullのフィールドがさらに存在する可能性があります。 したがって、vw_employee_current_appointment_infoのNULLがuddsのレコード数ここでdivisionはNULLなので、それらはすべて戻ってきます。等しい結合でこれを試すと、その数は最大319になります。

+0

-1 'OR udds IS NULL'ならあなたが説明したことは意味があります –

1

あなたのテーブルに左結合を使用しました。結合条件がtrueの場合、選択されたものよりもすべてのテーブルに一致するレコードが1つずつ左テーブルのレコードをフェッチします。この場合null条件がtrueです。結果セット・ビューを繰り返す

関連する問題