SELECT
	id,
	receivable_id,
	case when sale_id > 0 then  1 when receiving_id > 0 then 2 when carry_remain_id > 0 then 3 END as deal_type_code,
	sale_id as trade_id,
	receiving_id as payment_id,
	carry_remain_id as carry_id,
	NOW() as created_at,
	NOW() as updated_at
FROM receivable_details
INTO OUTFILE "/tmp/receivable_details.csv" FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"';