开发者

Counting Subqueries

开发者 https://www.devze.com 2023-01-15 23:47 出处:网络
My last quest was too vague, so I will revise it.I know th开发者_JAVA百科e following isn\'t correct SQL syntax, however you should be able to get the gist of the query I\'m trying to perform.

My last quest was too vague, so I will revise it. I know th开发者_JAVA百科e following isn't correct SQL syntax, however you should be able to get the gist of the query I'm trying to perform.

select id, 
       title, 
       count(select x 
              from otherTable 
             where otherTable.id = thisTable.id) as new_row 
 from thisTable

I hope this is a bit better explained.


select tt.id, tt.title, count(ot.id) as count
from thisTable tt
inner join otherTable ot on ot.id = tt.id
group by tt.id, tt.title


Another solution. If you want to know the row count, and not the number of distinct x values, then use count(*) instead of count(distinct x).

select id, title,
    (select count(distinct x) from otherTable
    where otherTable.id = thisTable.id) as new_row 
from thisTable
0

精彩评论

暂无评论...
验证码 换一张
取 消