I have three tables
usrs
ID Name
--------
1 Mark Dav
2 Dan Fos
cust
ID Name
--------
1 Jane Cue
2 Mary Tie
msgs
ID Frm_ID To_ID Msg frm_cat to_cat
--------
1 1 1 hello usr cus
1 1 2 There usr cus
1 2 1 hello cus usr
1 2 2 hello cus usr
How do I join the tables so that I can get following results.
msgs
ID Frm_ID To_ID Frm_name To_name Msg frm_cat to_cat
------------------------------------------------
1 1 1 Mark Dav Jane Cue hello usr cus
1 1 2 Mark Dav Mary Tie There usr cus
1 2 1 Mark Tie Mark Dav hello cus usr
1 2 2 Mark Tie Dan Fos hello cus usr
my attempt
id as id,
m.frm_id,
m.to_id,
m.msg,
f.name frm_name,
t.name to_name,
from msgs m
left join usrs on f.id = m.from_id AND m.from_cat = 'usr' OR cust f on f.id = m.frm_id AND m.from_cat = 'cus'
left join usrs t on t.id = m.to_id AND m.from_cat = 'usr' OR cust f on f.id = m.fom_id AND m.from_cat = 'cus';
but i got series of errors