salix/db/routines/vn/views/clientPhoneBook.sql

42 lines
1007 B
SQL

CREATE OR REPLACE DEFINER=`vn`@`localhost`
SQL SECURITY DEFINER
VIEW `vn`.`clientPhoneBook`
AS SELECT `c`.`id` AS `clientFk`,
trim(`c`.`phone`) AS `phone`
FROM `vn`.`client` `c`
WHERE `c`.`phone` <> 0
AND `c`.`isActive` <> 0
UNION
SELECT `c`.`id` AS `clientFk`,
trim(`c`.`mobile`) AS `phone`
FROM `vn`.`client` `c`
WHERE `c`.`mobile` <> 0
AND `c`.`isActive` <> 0
UNION
SELECT `a`.`clientFk` AS `clientFk`,
trim(`a`.`phone`) AS `phone`
FROM (
`vn`.`address` `a`
JOIN `vn`.`client` `c` ON(`c`.`id` = `a`.`clientFk`)
)
WHERE `a`.`phone` <> 0
AND `c`.`isActive` <> 0
UNION
SELECT `a`.`clientFk` AS `clientFk`,
trim(`a`.`mobile`) AS `phone`
FROM (
`vn`.`address` `a`
JOIN `vn`.`client` `c` ON(`c`.`id` = `a`.`clientFk`)
)
WHERE `a`.`mobile` <> 0
AND `c`.`isActive` <> 0
UNION
SELECT `cc`.`clientFk` AS `clientFk`,
trim(`cc`.`phone`) AS `phone`
FROM (
`vn`.`clientContact` `cc`
LEFT JOIN `vn`.`client` `c` ON(`c`.`id` = `cc`.`clientFk`)
)
WHERE `c`.`phone` <> 0
AND `c`.`isActive` <> 0