I have a coin table which provides all the information about uploaded coins, and there is an owner table which gives information about the owner of a particular coin. In the coin_owners table, there is field 'ownership_mode' which will be either 'transfer' or 'Reward'. If the mode is 'transfer', I want to left join the transfer table, and if the mode is 'Reward', I want to join the rewards table.
I have tried this using the CASE clause, but it's not working.
The query I tried is:
SELECT * , coins.id AS CoinId FROM coins
LEFT JOIN coin_owners ON coin_owners.coin_id = coins.id
LEFT JOIN (CASE WHEN coin_owners.`ownership_mode` = 'transfer' THEN transfer_detail ON transfer_detail.transfer_to = coin_owners.current_owner
AND transfer_detail.transfer_from = SUBSTRING_INDEX( coin_owners.previous_owner, ',', '-1' )
WHEN coin_owners.`ownership_mode` = 'Reward' THEN rewards ON rewards.`coin_ids` = coin_owners.coin_id
AND coin_owners.`ownership_mode` = 'Reward'
AND STATUS = '0'
) WHERE coins.id ='".validString($_REQUEST["coinId"])."' LIMIT 0,1