ALTER ALGORITHM = UNDEFINED DEFINER = `root`@`localhost` SQL SECURITY
DEFINER VIEW `patient_dentist` AS
SELECT `patients`.`office_id` AS `office_id`
,`patients`.`id` AS `patient_id`
,`patients`.`dentist_id` AS `dentist_id`
,`patients`.`dentist2_id` AS `dentist2_id`
,`patients`.`dentist3_id` AS `dentist3_id`
FROM `patients`
WHERE (
(`patients`.`dentist_id` IS NOT NULL)
AND isnull(`patients`.`deleted_at`)
)
UNION
SELECT DISTINCT `reservations`.`office_id` AS `office_id`
,`reservations`.`patient_id` AS `patient_id`
,`reservations`.`staff_id` AS `dentist_id`
FROM `reservations`
WHERE (
(`reservations`.`patient_id` IS NOT NULL)
AND (`reservations`.`staff_id` IS NOT NULL)
AND isnull(`reservations`.`deleted_at`)
);
我在'病人'表中添加了两个字段,dentist2_id
和dentist3_id
。
答案 0 :(得分:1)
您应该在代码的两个部分中添加这些列,因为UNION需要相同数量的列。如果您没有这些列,请尝试添加一些虚拟值
ALTER ALGORITHM = UNDEFINED DEFINER = `root`@`localhost` SQL SECURITY
DEFINER VIEW `patient_dentist` AS
SELECT `patients`.`office_id` AS `office_id`
,`patients`.`id` AS `patient_id`
,`patients`.`dentist_id` AS `dentist_id`
,`patients`.`dentist2_id` AS `dentist2_id`
,`patients`.`dentist3_id` AS `dentist3_id`
FROM `patients`
WHERE (
(`patients`.`dentist_id` IS NOT NULL)
AND isnull(`patients`.`deleted_at`)
)
UNION
SELECT DISTINCT `reservations`.`office_id` AS `office_id`
,`reservations`.`patient_id` AS `patient_id`
,`reservations`.`staff_id` AS `dentist_id`,
0,
0
FROM `reservations`
WHERE (
(`reservations`.`patient_id` IS NOT NULL)
AND (`reservations`.`staff_id` IS NOT NULL)
AND isnull(`reservations`.`deleted_at`)
);