DROP TABLE IF EXISTS `v_account`;
DROP VIEW IF EXISTS `v_account`;

CREATE OR REPLACE VIEW `_db_account`.`v_account` AS 
SELECT m_account.*, m_friends.id_member_id, m_public.id_permit_id
FROM m_account
LEFT OUTER JOIN m_friends ON m_account.id_account = m_friends.id_account
LEFT OUTER JOIN m_public ON m_account.id_account = m_public.id_account
WHERE m_account.c_delete = 0 and m_friends.c_delete = 0 and m_public.c_delete = 0;

DROP TABLE IF EXISTS `v_friends`;
DROP VIEW IF EXISTS `v_friends`;

CREATE OR REPLACE VIEW `_db_account`.`v_friends` AS 
SELECT m_friends.*, m_account.c_account, m_account.c_handle, m_public.id_permit_type, r_permit_type.c_permit_type
FROM m_account INNER JOIN
m_public ON
m_account.id_account = m_public.id_account INNER JOIN
r_permit_type ON
m_public.id_permit_type = r_permit_type.id_permit_type RIGHT OUTER JOIN
m_friends ON m_public.id_permit_id = m_friends.id_account AND
m_account.id_account = m_friends.id_member_id;

DROP TABLE IF EXISTS `v_public`;
DROP VIEW IF EXISTS `v_public`;

CREATE OR REPLACE VIEW `_db_account`.`v_public` AS 
SELECT m_public.*, m_account.c_handle, r_permit_type.c_permit_type FROM m_public
LEFT OUTER JOIN m_account ON m_public.id_permit_id = m_account.id_account
LEFT OUTER JOIN r_permit_type ON m_public.id_permit_type = r_permit_type.id_permit_type
WHERE m_public.c_delete = 0;
