I’m making the following SQL call:
SELECT dpt_id,
dpt_title,
dpt_active,
dpt_parent
FROM tbl_department ORDER BY dpt_title
where dpt_parent is an integer value of a parent department id (the dpt_id of another department).
I’d like the result set of the above call to include the dpt_title of that dpt_parent. My initial attempt at this was as follows:
SELECT dpt_id,
dpt_title,
dpt_active,
dpt_parent,
(SELECT dpt_title
FROM tbl_department
WHERE tbl_department.dpt_id = dpt_parent
) AS parent_title
FROM tbl_department ORDER BY dpt_title
With the call in this form, all values of parent_title are NULL
I can see using the same table like this is confusing, what is the solution to this problem?
use a table alias:
but you’d be better off removing the sub select and just joining like: