MYSQL IF Function - mysql

I am using two joins on the same table for two different select statements. I am wanting to implement an IF function, due to the fact that the table may contain a zero, I am wanting to substitue the zero for the word 'None'
Here is my SQL that works:
SELECT
CONCAT(`payments`.`AssignedTo`," - ",`people2`.`FName`," ",`people2`.`LName`) AS `AssignedTo`,
CONCAT(`payments`.`PersonID`," - ",`people`.`FName`," ",`people`.`LName`) AS `PersonName`
FROM `mb_payments` as `payments`
LEFT JOIN `mb_people` AS `people` ON `people`.`PersonID` = `payments`.`PersonID`
LEFT JOIN `mb_people` AS `people2` ON `people2`.`PersonID` = `payments`.`AssignedTo`
Here is my SQL with the IF function that does not work:
SELECT
IF(AssignedTo IS 0,'None', CONCAT(`payments`.`AssignedTo`," - ",`people2`.`FName`," ",`people2`.`LName`)),
CONCAT(`payments`.`PersonID`," - ",`people`.`FName`," ",`people`.`LName`) AS `PersonName`
FROM `mb_payments` as `payments`
LEFT JOIN `mb_people` AS `people` ON `people`.`PersonID` = `payments`.`PersonID`
LEFT JOIN `mb_people` AS `people2` ON `people2`.`PersonID` = `payments`.`AssignedTo`

You can use CASE statement in this case or if you want to use IF statement then your statement is wrong.
It should be:
SELECT
IF(AssignedTo = 0,'None', CONCAT(`payments`.`AssignedTo`," - ",`people2`.`FName`," ",`people2`.`LName`)) AS Result,
CONCAT(`payments`.`PersonID`," - ",`people`.`FName`," ",`people`.`LName`) AS `PersonName`
FROM `mb_payments` as `payments`
LEFT JOIN `mb_people` AS `people` ON `people`.`PersonID` = `payments`.`PersonID`
LEFT JOIN `mb_people` AS `people2` ON `people2`.`PersonID` = `payments`.`AssignedTo`
Check out this blog: http://timmurphy.org/2009/08/13/inline-if-and-case-statements-in-mysql/

I think you want ".. = 0", not "... IS 0". You use IS when comparing with NULL.

Related

Left Join not returning Null or Non-matching values

I want to return all values from PF_OUTPUT where the token ID equals "AdmissionInfo" as well as the matching results from PF_RESULTS just for PAT_SEQ = 105566168.
I've looked at some other questions and I have included the null statement within the Left join where clause, however it still is just returning matching values.
SELECT CCDEV.PF_OUTPUT.TOKEN_NAME,
CCDEV.PF_OUTPUT.DISPLAY_SEQ,
CCDEV.PF_OUTPUT.RES_SEQ,
CCDEV.PF_OUTPUT.LABEL_SEQ,
CCDEV.PF_OUTPUT.DISPLAY_RESULT_NAME,
CCDEV.PF_OUTPUT.OUTPUT_RESULT_NAME,
CCDEV.PF_OUTPUT.OUTPUT_FIELD_NAME,
PF_RESULTS.PERFORM_DDT,
PF_RESULTS.CHART_DDT,
PF_RESULTS.STAFF_SEQ,
PF_RESULTS.RESULT_VALUE,
PF_RESULTS.STATUS,
PF_RESULTS.PF_RESULTS_SEQ,
PF_RESULTS.PAT_SEQ,
PF_RESULTS.PF_RESULT_SEQ,
PF_RESULTS.RECORD_VERSION,
PF_RESULTS.LABEL_SEQ AS LABEL_SEQ1
FROM CCDEV.PF_OUTPUT
LEFT JOIN PF_RESULTS
ON CCDEV.PF_OUTPUT.LABEL_SEQ = PF_RESULTS.LABEL_SEQ
AND CCDEV.PF_OUTPUT.RES_SEQ = PF_RESULTS.RES_SEQ
WHERE (CCDEV.PF_OUTPUT.TOKEN_NAME = 'AdmissionInfo'
AND PF_RESULTS.PAT_SEQ = 105566168)
OR (CCDEV.PF_OUTPUT.TOKEN_NAME = 'AdmissionInfo'
AND PF_RESULTS.PAT_SEQ IS NULL)
ORDER BY CCDEV.PF_OUTPUT.TOKEN_NAME,
CCDEV.PF_OUTPUT.DISPLAY_SEQ
Move PF_RESULTS.PAT_SEQ = 105566168 line from your where condition to your ON clause of the join.
SELECT CCDEV.PF_OUTPUT.TOKEN_NAME,
CCDEV.PF_OUTPUT.DISPLAY_SEQ,
CCDEV.PF_OUTPUT.RES_SEQ,
CCDEV.PF_OUTPUT.LABEL_SEQ,
CCDEV.PF_OUTPUT.DISPLAY_RESULT_NAME,
CCDEV.PF_OUTPUT.OUTPUT_RESULT_NAME,
CCDEV.PF_OUTPUT.OUTPUT_FIELD_NAME,
PF_RESULTS.PERFORM_DDT,
PF_RESULTS.CHART_DDT,
PF_RESULTS.STAFF_SEQ,
PF_RESULTS.RESULT_VALUE,
PF_RESULTS.STATUS,
PF_RESULTS.PF_RESULTS_SEQ,
PF_RESULTS.PAT_SEQ,
PF_RESULTS.PF_RESULT_SEQ,
PF_RESULTS.RECORD_VERSION,
PF_RESULTS.LABEL_SEQ AS LABEL_SEQ1
FROM CCDEV.PF_OUTPUT
LEFT JOIN PF_RESULTS
ON CCDEV.PF_OUTPUT.LABEL_SEQ = PF_RESULTS.LABEL_SEQ
AND CCDEV.PF_OUTPUT.RES_SEQ = PF_RESULTS.RES_SEQ
AND PF_RESULTS.PAT_SEQ = 105566168
WHERE (CCDEV.PF_OUTPUT.TOKEN_NAME = 'AdmissionInfo')
OR (CCDEV.PF_OUTPUT.TOKEN_NAME = 'AdmissionInfo'
AND PF_RESULTS.PAT_SEQ IS NULL)
ORDER BY CCDEV.PF_OUTPUT.TOKEN_NAME,
CCDEV.PF_OUTPUT.DISPLAY_SEQ
When you include a column from the RIGHT table of your LEFT join you essentially eliminate the LEFT join and make it an inner join. Unless you are saying where RIGHTTable.column IS NULL.
It looks like you're using the left-joined table in the where clause. This effectively makes it an inner join since any record that doesn't exist wil always yield false.
Unless of course the check in the where clause includes an 'is null' check.
You should move the PF_RESULTS.PAT_SEQ = 105566168 check to the on clause of the left join to fix this.

Add join to SQL query

I need to add a join 'level' to a pre-existing SQL query... unfortunately I keep getting errors with this query. I'm proabably falling somewhere but I cannot understand how to fix it.
The original query is the following:
SELECT
noleggio.*,
nome AS convenzionato
FROM
anag_convenzionati
RIGHT JOIN (SELECT
noleggio.*, targa, dc_standard AS dcstandard
FROM
veicoli_contratti
RIGHT JOIN (SELECT
noleggio.*,
nome AS assicurazione_pagante
FROM
anag_assicurazioni
RIGHT JOIN (SELECT
fatt_sconto_noleggio,
fatt_prezzo_noleggio,
id AS idnoleggio,
numero,
serie,
id_convenzionato,
stato_noleggio,
modalita_noleggio,
conducente,
locatario,
locazione_in_proprio,
id_assicurazione_pagante,
id_veicolo,
giorni,
fatt_giorni_noleggio,
fatt_prezzo_totale_noleggio,
data_pagamento_cliente_a_convenzionato,
ore_manodopera,
IF(locazione_in_proprio = 1, conducente, locatario) AS cedente
FROM
noleggio_veicoli
WHERE
((data_cancellazione IS NULL) OR (data_cancellazione = ''))
) AS noleggio ON noleggio.id_assicurazione_pagante = anag_assicurazioni.id
) AS noleggio ON noleggio.id_veicolo = veicoli_contratti.id
) AS noleggio ON noleggio.id_convenzionato = anag_convenzionati.id;
I need to join the resulting table with moduli_ocr table in this way:
SELECT
noleggio.*
FROM
(//query//) AS noleggio
LEFT JOIN
moduli_ocr ON moduli_ocr.id_noleggio = noleggio.id;
Where //query// is the code above.
The error I got (running the query in MySQL Workbench is:
Error Code: 1054. Unknown column 'noleggio.id' in 'on clause'
BTW I'm not sure if I have to use RIGHT or LEFT join but I will check this once the query is properly 'running'.
Best regards.
It seems that you assign idnoleggio alias to the field "id". Try joining on
moduli_ocr.id_noleggio = noleggio.idnoleggio;

mysql select from a view with where condition gives different result than executing the view definition with where condition

I have a view with the following definition:
select
`cb_trans_detail`.`numero_partida` AS `numero_partida`,
`cb_trans_head`.`fecha_partida` AS `fecha_partida`,
`cb_trans_detail`.`concepto_partida` AS `concepto_partida`,
`cb_cuenta`.`nombre_cuenta` AS `nombre_cuenta`,
`cb_cuenta`.`codigo_mayor` AS `codigo_mayor`,
`cb_mayor`.`nombre_mayor` AS `nombre_mayor`,
`cb_mayor`.`categoria` AS `categoria`,
`cb_categoria`.`nombre` AS `nombre`,
`cb_categoria`.`presentacion` AS `presentacion`,
`cb_trans_detail`.`codigo_cuenta` AS `codigo_cuenta`,
sum(`cb_trans_detail`.`debito_partida`) AS `Debitos`,
sum(`cb_trans_detail`.`credito_partida`) AS `Creditos`,
`cb_cuenta`.`saldo_inicial` AS `saldo_inicial`,
((`cb_cuenta`.`saldo_inicial` +
sum(`cb_trans_detail`.`debito_partida`)) -
sum(`cb_trans_detail`.`credito_partida`)) AS `Saldo`,
concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`)
AS `Codigo`
from
((((`cb_trans_detail` join `cb_cuenta`
on(((`cb_trans_detail`.`codigo_cuenta` = `cb_cuenta`.`codigo_cuenta`)
and (`cb_trans_detail`.`categoria` = `cb_cuenta`.`categoria`)
and (`cb_trans_detail`.`codigo_mayor` = `cb_cuenta`.`codigo_mayor`))))
join `cb_mayor` on(((`cb_cuenta`.`codigo_mayor` = `cb_mayor`.`codigo_mayor`)
and (`cb_cuenta`.`categoria` = `cb_mayor`.`categoria`))))
join `cb_categoria` on(((`cb_mayor`.`categoria` = `cb_categoria`.`categoria`)
and (`cb_trans_detail`.`categoria` = `cb_categoria`.`categoria`))))
left join `cb_trans_head` on((`cb_trans_detail`.`numero_partida` =
`cb_trans_head`.`numero_partida`)))
where
(`cb_categoria`.`presentacion` = '1')
group by concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,
`cb_trans_detail`.`codigo_cuenta`)
If I select from this view as follows:
SELECT
`balance_general_view`.`numero_partida`,
`balance_general_view`.`fecha_partida`,
`balance_general_view`.`concepto_partida`,
`balance_general_view`.`nombre_cuenta`,
`balance_general_view`.`codigo_mayor`,
`balance_general_view`.`nombre_mayor`,
`balance_general_view`.`categoria`,
`balance_general_view`.`nombre`,
`balance_general_view`.`presentacion`,
`balance_general_view`.`codigo_cuenta`,
`balance_general_view`.`Debitos`,
`balance_general_view`.`Creditos`,
`balance_general_view`.`saldo_inicial`,
`balance_general_view`.`Saldo`,
`balance_general_view`.`Codigo`
FROM
`balance_general_view`
WHERE
`balance_general_view`.`fecha_partida` BETWEEN '2014-01-01' AND '2014-01-31'
this yields a different result than if I execute the query as follows:
select
`cb_trans_detail`.`numero_partida` AS `numero_partida`,
`cb_trans_head`.`fecha_partida` AS `fecha_partida`,
`cb_trans_detail`.`concepto_partida` AS `concepto_partida`,
`cb_cuenta`.`nombre_cuenta` AS `nombre_cuenta`,
`cb_cuenta`.`codigo_mayor` AS `codigo_mayor`,
`cb_mayor`.`nombre_mayor` AS `nombre_mayor`,
`cb_mayor`.`categoria` AS `categoria`,
`cb_categoria`.`nombre` AS `nombre`,
`cb_categoria`.`presentacion` AS `presentacion`,
`cb_trans_detail`.`codigo_cuenta` AS `codigo_cuenta`,
sum(`cb_trans_detail`.`debito_partida`) AS `Debitos`,
sum(`cb_trans_detail`.`credito_partida`) AS `Creditos`,
`cb_cuenta`.`saldo_inicial` AS `saldo_inicial`,
((`cb_cuenta`.`saldo_inicial` + sum(`cb_trans_detail`.`debito_partida`)) - sum(`cb_trans_detail`.`credito_partida`)) AS `Saldo`,
concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`) AS `Codigo`
from
((((`cb_trans_detail` join `cb_cuenta` on(((`cb_trans_detail`.`codigo_cuenta` = `cb_cuenta`.`codigo_cuenta`) and (`cb_trans_detail`.`categoria` = `cb_cuenta`.`categoria`) and (`cb_trans_detail`.`codigo_mayor` = `cb_cuenta`.`codigo_mayor`)))) join `cb_mayor` on(((`cb_cuenta`.`codigo_mayor` = `cb_mayor`.`codigo_mayor`) and (`cb_cuenta`.`categoria` = `cb_mayor`.`categoria`)))) join `cb_categoria` on(((`cb_mayor`.`categoria` = `cb_categoria`.`categoria`) and (`cb_trans_detail`.`categoria` = `cb_categoria`.`categoria`)))) left join `cb_trans_head` on((`cb_trans_detail`.`numero_partida` = `cb_trans_head`.`numero_partida`)))
where
(`cb_categoria`.`presentacion` = '1') and `cb_trans_head`.`fecha_partida` BETWEEN '2014-01-01' and '2014-01-31'
group by
concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`)
My question is: How to get the result I need using the view and filtering programatically instead of hard-coding the where condition? Thank you. If you need the individual table definitions let me know. Much appreciated.
If you use a left join to a table X and a condition in the WHERE-clause to this table X, you change your left join to an inner one. If you want to restrict the results by the values of this left joined table, you must use this condition in the ON clause of the left join instead:
...
LEFT JOIN
cb_trans_head
ON
cb_trans_detail.numero_partida = cb_trans_head.numero_partida
AND
cb_trans_head.fecha_partida BETWEEN '2014-01-01' and '2014-01-31'
...
If there's another one that I overlook, tread it the same way.

Update Query Issues

Here is my update query.
UPDATE sugarcrm.qb_salesorders_leads_c, sugarcrm.qb_salesorders
SET sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsleads_ida = sugarcrm.qb_salesorders.memo, sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb = sugarcrm.qb_salesorders.id
WHERE sugarcrm.qb_salesorders.id = sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb
When I run this it gives me Affected rows 0.
Here is my select statement using the same info that is in the WHERE statement.
SELECT * from qb_salesorders_leads_c, sugarcrm.qb_salesorders
WHERE sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb = sugarcrm.qb_salesorders.id
This returns 354 Rows which is what I am expecting to update on the update query. What am I missing. Please help!
Convert the implicit join to an explicit one:
UPDATE sugarcrm.qb_salesorders_leads_c leads
INNER JOIN sugarcrm.qb_salesorders orders
ON orders.id = leads.qb_salesorders_leadsqb_salesorders_idb
SET leads.qb_salesorders_leadsleads_ida = orders.memo,
leads.qb_salesorders_leadsqb_salesorders_idb = orders.id
As you can see, I also used aliases to make the SQL compact and legible.
To UPDATE using a join, you have to use the explicit join syntax:
UPDATE sugarcrm.qb_salesorders_leads_c
INNER JOIN sugarcrm.qb_salesorders
ON qb_salesorders_leadsqb_salesorders_idb.id
= sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb
SET sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsleads_ida
= qb_salesorders_leadsqb_salesorders_idb.memo
, sugarcrm.qb_salesorders_leads_c.qb_salesorders_leadsqb_salesorders_idb
= sugarcrm.qb_salesorders.id

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
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
You are using left joins so your base table connects to the subsequent tables even if no value exists. However, when you begin to filter the subsequent tables you are specifying a that the subsequent table contain certain values. In this case there are probably only 319 values in the "vw_employee_current_appointment_info" with a null in the division field. When you add in the udds field there can be more fields that are null, because of the left join the division field is NULL by definition
So, the number of connections where "vw_employee_current_appointment_info" has udds as NULL is greater than the number of records where division is NULL, so it brings them all back. If you try this with equal joins then the number will be 319 max.