I have a table which has the following structure:
id name id_relation
---------------------------
1 this NULL
2 that 1
I want a query to get, instead of id_relation, the name of the correlating id (in this case – ‘this’, so at the end, I’ll get this result:
id name parent_name
-----------------------
2 that this
Is it possible to do this?
Yes. Join the table to itself:
This query will return rows for every row in your table, even if there isn’t a matching “relation” row.
If you want to restrict the result rows to only those that have a matching row (as your example suggests), remove the
LEFTkeyword.