DROP TABLE IF EXISTS `v_abook`;
DROP VIEW IF EXISTS `v_abook`;

CREATE OR REPLACE VIEW `_db_abook`.`v_abook` AS 
SELECT m_abook.*, m_category.c_categoryName, m_category.c_categoryDisplayOrder, m_category.c_categoryIcon, r_kenmei.c_kenmei
FROM m_abook
LEFT OUTER JOIN m_category ON m_abook.id_category = m_category.id_category
LEFT OUTER JOIN r_kenmei ON m_abook.c_kenid = r_kenmei.c_kenid
WHERE m_abook.c_delete = 0;

DROP TABLE IF EXISTS `v_zip100`;
DROP VIEW IF EXISTS `v_zip100`;

CREATE OR REPLACE VIEW `_db_abook`.`v_zip100` AS 
SELECT r_zip100.*, r_kenmei.c_kenid
FROM r_zip100
LEFT OUTER JOIN r_kenmei ON r_zip100.c_kenmei = r_kenmei.c_kenmei;

DROP TABLE IF EXISTS `v_abook_chk_ken`;
DROP VIEW IF EXISTS `v_abook_chk_ken`;

CREATE OR REPLACE VIEW `_db_abook`.`v_abook_chk_ken` AS 
SELECT m_abook.id_account, m_abook.id_abook, m_abook.c_zip1, m_abook.c_kenid, m_abook.c_address1, v_zip100.c_zip100 as c_zip_zip100, v_zip100.c_kenid as c_zip_kenid, v_zip100.c_kenmei as c_zip_kenmei
FROM m_abook
JOIN v_zip100 ON substring(m_abook.c_zip1,1,2) = v_zip100.c_zip100
WHERE m_abook.c_kenid = '' and m_abook.c_zip1 <> '';
