Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL view returns more results with an AND?

When I do this:

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

Makes sense. And, then I do this...

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

Whaaaaat?? MORE results??

So, I had to wrap the view as a subselect for it to return what I was expecting. Like this:

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

Can someone explain this? I've never had to do this in MS SQLSERVER - so this is foreign to me.



The SQL used to create this view actually joins a number of other views together. I'm not entirely sure it's helpful to see, but you asked for it :) It's basically

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

And the vw_employee_current_appointment_info which contains 'division' and 'udds' looks like:

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
like image 967
bmherold Avatar asked Dec 08 '11 21:12

bmherold


People also ask

What is %s and %D in MySQL?

12 years, 11 months ago. it's for php to know how to handle the parameters, %d – the argument is treated as an integer, and presented as a (signed) decimal number. %s – the argument is treated as and presented as a string. in your examples, $slug is a string and $this->id is an integer.

What is the difference in AND & OR operator in MySQL?

The difference between AND, OR is that AND evaluates both conditions must be true for the overall condition to be true. The OR evaluates one condition must be true for the overall condition to be true. In the OR result, if name is John then condition will be true. If any row has the age 22, then it will be true.

What does /* mean in MySQL?

The /* is the beginning of a comment and */ is the end of comment. Let us implement and display how to create a comment mysql> /* This is the first MySQL Program */ MySQL will ignore the above comment.

Which function returns the number of occurrences MySQL?

MySQL COUNT() Function The COUNT() function returns the number of records returned by a select query.


1 Answers

you have used left join in your tables .when join condition is true than it has been select records those match in all tables than fetch records from left tables one by one and in this case your null condition is true and it is repeat the result set view

like image 87
Pradeep Sharma Avatar answered Oct 20 '22 00:10

Pradeep Sharma