mybatis/mybatis-dynamic-sql

Support Sub-Queries in Select List?

Closed this issue · 2 comments

if i want to write sql like this,

select id, (select count(*) from sub) as cnt from main where id=?

what should i do?

That query doesn't really make sense. I assume you want to know the number of rows in sub that have the same id as main - not the total rows in sub. But I think I understand the larger question.

We don't support sub-queries in the select list right now, but it should be relatively simple to add it.

In the mean time, it might be helpful to think of this particular query as a join (which it actually is). You could code it like this:

select main.id, count(*) as cnt
from main join sub on main.id = sub.id
group by main.id
where main.id = ?

Sorry, I forgot to mention some conditions. Subqueries are more intuitive to me. Thank you for your reply.