I have 2 tables in a SQLite file organized as follows:
- tNode which contains a node name
- tItem which contains a name, a priority, and the reference to its parent node. A node can have multiple items.
To list the nodes and their items, I just have to make a simple JOIN query :
SELECT tNode.node, ..., tItem.itemName, tItem.itemPriority, ...
FROM tNode
LEFT JOIN tItem ON tItem.node=tNode.node
ORDER BY tNode.node
But now I want the list of the nodes including the item regardless of its priority (without any duplicate node in the list). I thought I could use the following query:
SELECT tNode.node, ..., tItem2.itemName, tItem2.itemPriority, ...
FROM tNode
LEFT JOIN
(SELECT itemName,itemPriority FROM tItem WHERE tItem.node=tNode.node LIMIT 1)
AS tItem2 ON tItem2.node=tNode.node
ORDER BY tNode.node
But this doesn’t work (“no such column: tNode.node”). It seems I can’t use the tNode.node column in the subquery.
How can I achieve this without having to make one subquery per column?
try after removing where clause in subQuery, also LIMIT and adding group by with min