2011-12-08 11 views
8

Cuando hago esto:MySQL view devuelve más resultados con un AND?

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

tiene sentido. Y, entonces hago esto ...

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

Whaaaaat ?? ¿¿Más resultados??

Entonces, tuve que ajustar la vista como una subselección para que me devolviera lo que esperaba. De esta manera:

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

¿Alguien puede explicar esto? Nunca tuve que hacer esto en MS SQLSERVER, así que esto es extraño para mí.



El SQL utilizada para crear esta imagen en realidad se une a una serie de otros puntos de vista juntos. No estoy completamente seguro de que es muy útil para ver, pero tú lo has querido :) Se trata básicamente de

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 

Y el vw_employee_current_appointment_info que contiene 'división' y se ve 'UDDS' como:

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

favor nos muestran la vista (lo consulta usó para crear la vista vw_active_employees?) – cristian

+0

añadido la vista crear declaraciones anteriores de las opiniones pertinentes. Parece que el 'udds es nulo' de alguna manera se filtra y afecta la vista subyacente sql, lo cual me parece incorrecto. Desde mi entendimiento de las vistas, deberían ser una caja negra con resultados establecidos, y los criterios que apliquen no deberían afectar las consultas subyacentes que crean el conjunto de datos de la vista. – bmherold

+0

El manual dice que "IS" tiene mayor prioridad que "Y", pero ha intentado: 'donde (división IS NULL) AND (udds IS NULL);' –

Respuesta

-1

Usted están utilizando uniones izquierdas para que su tabla base se conecte a las tablas subsiguientes incluso si no existe ningún valor. Sin embargo, cuando comienza a filtrar las tablas subsiguientes, especifica que la tabla siguiente contiene ciertos valores. En este caso, probablemente haya solo 319 valores en "vw_employee_current_appointment_info" con nulo en el campo de división. Cuando agrega en el campo udds puede haber más campos que son nulos, debido a la combinación izquierda, el campo de división es NULL por definición Entonces, el número de conexiones donde "vw_employee_current_appointment_info" tiene udds como NULL es mayor que el número de registros donde la división es NULL, entonces los devuelve a todos. Si prueba esto con uniones iguales, entonces el número será 319 máximo.

+0

-1 Lo que describiste tiene sentido si fue 'O udds IS NULL' –

1

ha utilizado la combinación izquierda en sus tablas. Cuando la condición de unión es verdadera de lo que ha sido seleccionado, registra aquellas coincidencias en todas las tablas que recuperan registros de las tablas una a una y en este caso su condición nula es verdadera y es repetir el conjunto de resultados de vista

Cuestiones relacionadas