tags:

views:

57

answers:

2

My last quest was too vague, so I will revise it. I know the 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.

+3  A: 
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
be here now
I hesitate to vote for ANSI-89 join syntax, but otherwise 100% correct.
OMG Ponies
easier to fix than be offended.
Evan Carroll
@Evan Carroll: Zee brackets, zay do nuTHING! :) +1
OMG Ponies
hey guys may I still touch my own answer? ))
be here now
A left join instead of an inner join would show thisTable records with no matching otherTable records.
Mark Bannister
+1  A: 

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
Thomas Mueller