Skip to content

Instantly share code, notes, and snippets.

@csdear
Last active March 7, 2016 15:06
Show Gist options
  • Select an option

  • Save csdear/6cb4d62fb063aed98028 to your computer and use it in GitHub Desktop.

Select an option

Save csdear/6cb4d62fb063aed98028 to your computer and use it in GitHub Desktop.
LEFT join Template
--A LEFT join example.
-- Join two aliased table on two keys they share in common
-- Further filtering via 'and' commands.
--1.
select * from modem m
left join camera c
on m.modem_id = c.modem_id
left join camera_value cv
on c.camera_id = cv.camera_id
where m.user_id = 1
and cv.camera_setting_id = 40
and cv.value = 'SuperCam'
--2. A simple table joined to compare modem ownershipt
-- Since the common link between the two tables was ambigoulsy named 'SN'
-- each were then aliased to make readiblility better.
-- <<index.key>> AS 'NAME',
-- Does anyone own this modem?
SELECT sm.SN as 'SN_MEID Table SN', mo.SN as 'Modem Table SN', sm.meid as 'Associated MEID'
FROM SN_MEID SM
LEFT JOIN modem mo
ON sm.SN = mo.SN
--3. Another example, lots of columns cited.
-- Also what I like about this is the bottom >- filter, as I wante a certain set of audits to look up.
SELECT audit_id, al.audit_type, user_id, admin_id, description, message, update_on
FROM audit_log al
LEFT JOIN audit_log_type alt
ON al.audit_type = alt.audit_type
WHERE audit_id >= '45'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment