Re: [nyphp-talk] MySQL: count()children on 2 related tables in 1 query

2011-01-05 Thread SyAD
Is this possible in MySQL?: SELECT parent.id, parent.someColumn, (SELECT count(*) FROM child_table_1 as c1 WHERE c1.parent_id = parent.id) as cnt1, (SELECT count(*) FROM child_table_2 as c2 WHERE c2.parent_id = parent.id) as cnt2 FROM parent Not sure if this would give yo

Re: [nyphp-talk] MySQL: count()children on 2 related tables in 1 query

2011-01-04 Thread Rob Marscher
On Jan 4, 2011, at 4:50 PM, Dan Cech wrote: > Rob, > > This is demonstrating 2 special cases which will work properly: > > 1. no rows for a parent in one of the child tables > 2. 1 row for a parent in both child tables > > If you run your child inserts again to create more rows you'll end up wit

Re: [nyphp-talk] MySQL: count()children on 2 related tables in 1 query

2011-01-04 Thread Dan Cech
Rob, On 1/4/2011 4:28 PM, Rob Marscher wrote: insert into parent (name) values ('One'),('Two'),('Three'); insert into child1 (parent_id) values (1),(1),(1),(3); insert into child2 (parent_id) values (2),(2),(2),(3); explain select parent.id, parent.name, count(child1.id), count(child2.id) from

Re: [nyphp-talk] MySQL: count()children on 2 related tables in 1 query

2011-01-04 Thread Rob Marscher
On Jan 4, 2011, at 4:02 PM, Dan Cech wrote: > Sorry, forgot the ON clauses: > > SELECT parent.id, parent.someColumn, c1.cnt, c2.cnt > FROM parent > LEFT JOIN ( >SELECT child_table_1.parent_id,count(child_table_1.id) as cnt >FROM child_table_1 >GROUP BY child_table_1.parent_id > ) AS

Re: [nyphp-talk] MySQL: count()children on 2 related tables in 1 query

2011-01-04 Thread Dan Cech
Sorry, forgot the ON clauses: SELECT parent.id, parent.someColumn, c1.cnt, c2.cnt FROM parent LEFT JOIN ( SELECT child_table_1.parent_id,count(child_table_1.id) as cnt FROM child_table_1 GROUP BY child_table_1.parent_id ) AS c1 ON c1.parent_id=parent.id LEFT JOIN ( SELECT child_ta

Re: [nyphp-talk] MySQL: count()children on 2 related tables in 1 query

2011-01-04 Thread David Mintz
On Tue, Jan 4, 2011 at 3:57 PM, Dan Cech wrote: > On 1/4/2011 3:56 PM, David Mintz wrote: > >> Guess what. The logic of my SQL must be wrong. I finally let it think for >> nearly 12 minutes and the results were f***ed up. >> > > .. snip > > > all those count() values that are the same in both co

Re: [nyphp-talk] MySQL: count()children on 2 related tables in 1 query

2011-01-04 Thread Dan Cech
On 1/4/2011 3:56 PM, David Mintz wrote: Guess what. The logic of my SQL must be wrong. I finally let it think for nearly 12 minutes and the results were f***ed up. .. snip all those count() values that are the same in both columns are way way too large. mysql does not understand what I mean.

Re: [nyphp-talk] MySQL: count()children on 2 related tables in 1 query

2011-01-04 Thread David Mintz
On Tue, Jan 4, 2011 at 3:44 PM, David Mintz wrote: > > > On Tue, Jan 4, 2011 at 1:33 PM, Rob Marscher > wrote: > >> Looks like you need an index on events.event_type_id >> and requests.event_type_id. I'm not seeing those in your indexes. I see >> event_type_id is part of the uniqueRequest inde

Re: [nyphp-talk] MySQL: count()children on 2 related tables in 1 query

2011-01-04 Thread Dan Cech
On 1/4/2011 1:05 PM, David Mintz wrote: I am trying to do something like this: SELECT parent.id, parent.someColumn, count(child_table_1.id), count( child_table_2.id) FROM parent LEFT JOIN child_table_1 ON child_table_1.parent_id = parent.id LEFT JOIN child_table_2 ON child_table_2.parent_id = pa

Re: [nyphp-talk] MySQL: count()children on 2 related tables in 1 query

2011-01-04 Thread Rob Marscher
> On Jan 4, 2011, at 1:05 PM, David Mintz wrote: > >> >> I am trying to do something like this: >> >> SELECT parent.id, parent.someColumn, count(child_table_1.id), >> count(child_table_2.id) FROM parent >> LEFT JOIN child_table_1 ON child_table_1.parent_id = parent.id >> LEFT JOIN child_table_

Re: [nyphp-talk] MySQL: count()children on 2 related tables in 1 query

2011-01-04 Thread David Mintz
On Tue, Jan 4, 2011 at 1:33 PM, Rob Marscher wrote: > Looks like you need an index on events.event_type_id > and requests.event_type_id. I'm not seeing those in your indexes. I see > event_type_id is part of the uniqueRequest index, but it can't use it unless > it's the first column in the index

Re: [nyphp-talk] MySQL: count()children on 2 related tables in 1 query

2011-01-04 Thread Rob Marscher
Looks like you need an index on events.event_type_id and requests.event_type_id. I'm not seeing those in your indexes. I see event_type_id is part of the uniqueRequest index, but it can't use it unless it's the first column in the index or you specify the columns that come before it in your i

[nyphp-talk] MySQL: count()children on 2 related tables in 1 query

2011-01-04 Thread David Mintz
I am trying to do something like this: SELECT parent.id, parent.someColumn, count(child_table_1.id), count( child_table_2.id) FROM parent LEFT JOIN child_table_1 ON child_table_1.parent_id = parent.id LEFT JOIN child_table_2 ON child_table_2.parent_id = parent.id and it is so incredibly slow that