Reputation: 59
I am basically wanting to run a query that returns three (out of thousands) of tables with one specific field being unique.
I have been struggling with this for a while and I don't want to do it with three separate queries.
SELECT * FROM (
(SELECT cat_1_id, title FROM Article WHERE cat_1_id = 1 LIMIT 1) AS a,
(SELECT cat_1_id, title FROM Article WHERE cat_1_id = 2 LIMIT 1) AS b,
(SELECT cat_1_id, title FROM Article WHERE cat_1_id = 3 LIMIT 1) AS c
)
Does it count as one query or three?
Thank you, kind regards,
Matthew
Upvotes: 0
Views: 242
Reputation: 8104
are subqueries separate queries?
Sort of. In the same sense as joins are also sub-queries and so is group/having part of your query. on a high level engine builds "source" dataset and fetches data into output, subqueries extend the dataset or create additional datasets.
Depending on complexity engine will often do it's best to optimize and maybe even rule out creating additional datasets if the goal can be achieved by using indices or any other means, that is not always possible though.
Upvotes: 0
Reputation: 204884
It is one query containing 3 subqueries. But why not use
SELECT cat_1_id, min(title) as title
FROM Article
WHERE cat_1_id in (1,2,3)
group by cat_1_id
Upvotes: 6