mysql - Sql conditional Join on different tables -



mysql - Sql conditional Join on different tables -

i've add together notification module on existing project. table construction on pic.

picture :

as see on image every notification has type , releatedid.

type 1 = reservation cancelled, releated id id on "reservations" table

type 3 = business relationship balance under given min. limit releatedid id on "account_movements" table

what i'm trying conditional bring together avoid 2 different sql queries;

get notifications belong person get notification details different tables based on "notification.type"

so question can in 1 query ?

something next should work. specify records in notifications table bring together records in reservations table or account_movements table when bring together in. utilize left bring together of notification records create through, , records in reservations table or account_movements have match create through.

select n.id, n.type, n.companyid, n.personid, n.relatedid, n.description, r.details reservation_details, am.details account_movement_details, coalesce(r.details, am.details) combined_detail notifications n left outer bring together reservations r on n.relatedid = r.id , n.type = 1 left outer bring together account_movements on n.relatedid = am.id , n.type = 3

here sql fiddle solution well.

i added in coalesce() show since joins mutually exclusive, can safely combine columns reservations table , account_movements table single column without fear of missing or duplicating data.

mysql join condition

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -