Reputation: 3410
I have this medium-sized query and I am having some problems getting certain fields.
SELECT DISTINCT
enc.id, enc.cus_id, enc.createdon, enc.status,
enc.segment, enc.currentstep, enc.groupid, enc.fdprotocol,
enc_task.linkfile, cus.fname, cus.lname, login.first_name,
login.last_name, login.username, login.user_code, fp.protocol
FROM
mob_encounters_task enc_task, mob_encounters enc,
mob_customer cus, mob_login login, mob_protocol_type fp
WHERE
enc.id=enc_task.encounterid
AND
cus.id=enc_task.cus_id
AND
login.id=enc.createdby
GROUP BY enc.id
fp.protocol is a string, and on the table fp there are up to 5 or 6 "protocols".
what I wanted to do is if enc.fdprotocol is empty then fp.protocol should be empty, otherwise get the fp.protocol associated to the fp.id= enc.fdprotocol
Please let me know if this sounds confusing, I have been stuck on this for some time
Upvotes: 0
Views: 45
Reputation: 19309
I find that using the actual JOIN syntax makes queries much more readable and maintainable. In this case you need to use a LEFT JOIN
and you have to change your syntax for that:
SELECT enc.id, enc.cus_id, enc.createdon, enc.status, enc.segment,
enc.currentstep, enc.groupid, enc.fdprotocol, enc_task.linkfile, cus.fname,
cus.lname, login.first_name, login.last_name, login.username, login.user_code,
fp.protocol
FROM mob_encounters_task enc_task
JOIN mob_encounters enc ON enc.id=enc_task.encounterid
JOIN mob_customer cus ON cus.id=enc_task.cus_id
JOIN mob_login login ON login.id=enc.createdby
LEFT JOIN mob_protocol_type fp ON fp.id = enc.fdprotocol
Also I don't believe you need DISTINCT
Upvotes: 1