select * from m_entry left join m_template on m_entry.templateid = m_template.id where m_template.id is NULL
delete m_entry from m_entry left join m_template on m_entry.templateid = m_template.id where m_template.id is null