SET FOREIGN_KEY_CHECKS=0;
DELETE FROM tiba.users
WHERE id IN (
    SELECT u.id
    FROM (SELECT id FROM tiba.users WHERE id > 0) AS u
    LEFT JOIN staff s ON u.id = s.user_id
    WHERE s.user_id IS NULL
);
truncate table tiba.patients;
truncate table tiba.teeth;
truncate table tiba.mouth_parts;
truncate table tiba.multiple_teeths;
truncate table tiba.auto_by_passes;
truncate table tiba.bank_transfer_reconciliations;
truncate table tiba.by_pass_requests;
truncate table tiba.by_pass_responses;
truncate table tiba.by_passes;
truncate table tiba.callback_logs;
truncate table tiba.callbacks;
truncate table tiba.cash_reconciliations;
truncate table tiba.cheque_logs;
truncate table tiba.cheque_reconciliations;
truncate table tiba.cheques;
truncate table tiba.discounts;
truncate table tiba.draft_planned_treatment_levels;
truncate table tiba.draft_planned_treatment_sessions;
truncate table tiba.draft_planned_treatments;
truncate table tiba.draft_proposal_plans;
truncate table tiba.event_custom_details;
truncate table tiba.event_diagnosis_details;
truncate table tiba.event_emergency_details;
truncate table tiba.event_general_details;
truncate table tiba.event_logs;
truncate table tiba.event_meeting_details;
truncate table tiba.event_recall_details;
truncate table tiba.event_service_details;
truncate table tiba.event_session_detail_sessions;
truncate table tiba.event_session_details;
truncate table tiba.event_sessions;
truncate table tiba.event_staff;
truncate table tiba.event_tag;
truncate table tiba.event_terminations;
truncate table tiba.event_tracks;
truncate table tiba.events;
truncate table tiba.installments;
truncate table tiba.invoices;
truncate table tiba.log_event_details;
truncate table tiba.log_events;
truncate table tiba.payment_groups;
truncate table tiba.payment_installments;
truncate table tiba.payment_plan_installments;
truncate table tiba.payment_plan_methods;
truncate table tiba.payment_plans;
truncate table tiba.payment_purchases;
truncate table tiba.payments;
truncate table tiba.planned_treatment_levels;
truncate table tiba.planned_treatment_sessions;
truncate table tiba.planned_treatments;
truncate table tiba.preliminary_plans;
truncate table tiba.proposal_plans;
truncate table tiba.purchases;
truncate table tiba.purchases_groups;
truncate table tiba.receipts;
truncate table tiba.reconciliation_versions;
truncate table tiba.reconciliations;
truncate table tiba.refunds;
truncate table tiba.surfaces;
truncate table tiba.to_the_box_reconciliations;
truncate table tiba.tooth_multiple_teeth;
truncate table tiba.treatment_plan_versions;
truncate table tiba.treatment_plans;
TRUNCATE TABLE tiba.patient_payees;
TRUNCATE TABLE tiba.tag_user;
TRUNCATE TABLE tiba.user_relatives;
TRUNCATE TABLE tiba.credit_cards;
TRUNCATE TABLE tiba.event_confirmations;
TRUNCATE TABLE tiba.department_reconciliations;
TRUNCATE TABLE tiba.patient_black_list_statuses;
TRUNCATE TABLE tiba.additional_charges;
TRUNCATE TABLE tiba.event_confirmations;
TRUNCATE TABLE tiba.patient_black_list_statuses;
TRUNCATE TABLE tiba.department_reconciliations;
TRUNCATE TABLE tiba.recurring_event_rules;
TRUNCATE TABLE tiba.google_auths;
TRUNCATE TABLE tiba.google_syncs;
TRUNCATE TABLE tiba.google_sync_calendars;
TRUNCATE TABLE tiba.google_sync_google_calendars;
TRUNCATE TABLE tiba.google_sync_event_data;
TRUNCATE TABLE tiba.google_synced_events;
TRUNCATE TABLE tiba.google_sync_managers;
TRUNCATE TABLE tiba.events_google_events;
TRUNCATE TABLE tiba.patient_bypass_statuses;
TRUNCATE TABLE tiba.procedures;
TRUNCATE TABLE tiba.procedure_staff;
TRUNCATE TABLE tiba.event_procedure;
TRUNCATE TABLE tiba.planned_services;
TRUNCATE TABLE tiba.draft_planned_services;
TRUNCATE TABLE tiba.planned_diagnoses;
TRUNCATE TABLE tiba.draft_planned_diagnoses;
SET FOREIGN_KEY_CHECKS=1;