speedest way to handle a select table1,count(table2) where table1.idtable1=table2.idtable1 ?

This is a discussion on speedest way to handle a select table1,count(table2) where table1.idtable1=table2.idtable1 ? within the MySQL Database forums, part of the Database Forums category; Hello I have a list of item in TABLE1, and a list of sub item in TABLE2 each item of ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-14-2007
patrice
 
Posts: n/a
Default speedest way to handle a select table1,count(table2) where table1.idtable1=table2.idtable1 ?

Hello

I have a list of item in TABLE1, and a list of sub item in TABLE2
each item of TABLE1 can have 0 or more sub item

i need a query :
select TABLE1.x, count( TABLE2.subitem)

i guess the natural way should a TABLE1 left outer join TABLE2 with a
count(*) and a group by TABLE1.*
but i dont like the "group by table1.* "

mysql does not like correlated query in from, so i can not write :
select TABLE1.*, nbsubitem from TABLE1, (select count(*) as nbsubitem from
TABLE2 where TABLE2.iditem=TABLE1.iditem)

so is there a rewriting of this imaginary query that can produce the desired
column ?


Reply With Quote
  #2 (permalink)  
Old 06-14-2007
Captain Paralytic
 
Posts: n/a
Default Re: speedest way to handle a select table1,count(table2) where table1.idtable1=table2.idtable1 ?

On 14 Jun, 09:36, "patrice" <patrice_labracherie_nos...@free.fr>
wrote:
> Hello
>
> I have a list of item in TABLE1, and a list of sub item in TABLE2
> each item of TABLE1 can have 0 or more sub item
>
> i need a query :
> select TABLE1.x, count( TABLE2.subitem)
>
> i guess the natural way should a TABLE1 left outer join TABLE2 with a
> count(*) and a group by TABLE1.*
> but i dont like the "group by table1.* "

Why don't you like this?
Why would you group by table1.* when you are querying table1.x?

> mysql does not like correlated query in from, so i can not write :
> select TABLE1.*, nbsubitem from TABLE1, (select count(*) as nbsubitem from
> TABLE2 where TABLE2.iditem=TABLE1.iditem)

Why is this any better than a LEFT JOIN?

> so is there a rewriting of this imaginary query that can produce the desired
> column ?

Why do you want to re-write? What uis wrong with the left join?

Reply With Quote
  #3 (permalink)  
Old 06-14-2007
patrice
 
Posts: n/a
Default Re: speedest way to handle a select table1,count(table2) where table1.idtable1=table2.idtable1 ?

"Captain Paralytic" <paul_lautman@yahoo.com> a écrit dans le message de
news:1181810746.693039.245080@z28g2000prd.googlegr oups.com...
> On 14 Jun, 09:36, "patrice" <patrice_labracherie_nos...@free.fr>
> wrote:
> > Hello
> >
> > I have a list of item in TABLE1, and a list of sub item in TABLE2
> > each item of TABLE1 can have 0 or more sub item
> >
> > i need a query :
> > select TABLE1.x, count( TABLE2.subitem)
> >
> > i guess the natural way should a TABLE1 left outer join TABLE2 with a
> > count(*) and a group by TABLE1.*
> > but i dont like the "group by table1.* "

> Why don't you like this?
> Why would you group by table1.* when you are querying table1.x?


oops, i just this :
select TABLE1.* ,count(TABLE2.IDT2) from TABLE1 left outer join TABLE2 using
(IDT1) group by TABLE1.IDT1

and it works
i dont know why, but i was thinking that you need to use all select column
in the group by


Reply With Quote
  #4 (permalink)  
Old 06-14-2007
Pavel Lepin
 
Posts: n/a
Default Re: speedest way to handle a select table1,count(table2) where table1.idtable1=table2.idtable1 ?


patrice <patrice_labracherie_nospam@free.fr> wrote in
<467119d4$0$29914$426a34cc@news.free.fr>:
> "Captain Paralytic" <paul_lautman@yahoo.com> a écrit
>> On 14 Jun, 09:36, "patrice"
>> <patrice_labracherie_nos...@free.fr> wrote:
>> > I have a list of item in TABLE1, and a list of sub item
>> > in TABLE2 each item of TABLE1 can have 0 or more sub
>> > item
>> >
>> > i need a query :
>> > select TABLE1.x, count( TABLE2.subitem)
>> >
>> > i guess the natural way should a TABLE1 left outer join
>> > TABLE2 with a count(*) and a group by TABLE1.*
>> > but i dont like the "group by table1.* "

>>
>> Why don't you like this?
>> Why would you group by table1.* when you are querying
>> table1.x?

>
> oops, i just this :
> select TABLE1.* ,count(TABLE2.IDT2) from TABLE1 left outer
> join TABLE2 using (IDT1) group by TABLE1.IDT1
>
> and it works
> i dont know why, but i was thinking that you need to use
> all select column in the group by


The following might be of interest to you:

http://dev.mysql.com/tech-resources/...-by-myths.html

--
Pavel Lepin
Reply With Quote
  #5 (permalink)  
Old 06-14-2007
Captain Paralytic
 
Posts: n/a
Default Re: speedest way to handle a select table1,count(table2) where table1.idtable1=table2.idtable1 ?

On 14 Jun, 12:18, Pavel Lepin <p.le...@ctncorp.com> wrote:
> patrice <patrice_labracherie_nos...@free.fr> wrote in
> <467119d4$0$29914$426a3...@news.free.fr>:
>
>
>
>
>
> > "Captain Paralytic" <paul_laut...@yahoo.com> a ?crit
> >> On 14 Jun, 09:36, "patrice"
> >> <patrice_labracherie_nos...@free.fr> wrote:
> >> > I have a list of item in TABLE1, and a list of sub item
> >> > in TABLE2 each item of TABLE1 can have 0 or more sub
> >> > item

>
> >> > i need a query :
> >> > select TABLE1.x, count( TABLE2.subitem)

>
> >> > i guess the natural way should a TABLE1 left outer join
> >> > TABLE2 with a count(*) and a group by TABLE1.*
> >> > but i dont like the "group by table1.* "

>
> >> Why don't you like this?
> >> Why would you group by table1.* when you are querying
> >> table1.x?

>
> > oops, i just this :
> > select TABLE1.* ,count(TABLE2.IDT2) from TABLE1 left outer
> > join TABLE2 using (IDT1) group by TABLE1.IDT1

>
> > and it works
> > i dont know why, but i was thinking that you need to use
> > all select column in the group by

>
> The following might be of interest to you:
>
> http://dev.mysql.com/tech-resources/...group-by-myths...
>
> --
> Pavel Lepin- Hide quoted text -
>
> - Show quoted text -


But he was violating even the myth by wanting all the columns in the
group by.

Reply With Quote
  #6 (permalink)  
Old 06-14-2007
patrice
 
Posts: n/a
Default Re: speedest way to handle a select table1,count(table2) where table1.idtable1=table2.idtable1 ?

"Captain Paralytic" <paul_lautman@yahoo.com> a écrit dans le message de
news:1181820455.752483.208770@e9g2000prf.googlegro ups.com...
> On 14 Jun, 12:18, Pavel Lepin <p.le...@ctncorp.com> wrote:
> > patrice <patrice_labracherie_nos...@free.fr> wrote in
> > <467119d4$0$29914$426a3...@news.free.fr>:
> >
> >
> >
> >
> >
> > > "Captain Paralytic" <paul_laut...@yahoo.com> a ?crit
> > >> On 14 Jun, 09:36, "patrice"
> > >> <patrice_labracherie_nos...@free.fr> wrote:
> > >> > I have a list of item in TABLE1, and a list of sub item
> > >> > in TABLE2 each item of TABLE1 can have 0 or more sub
> > >> > item

> >
> > >> > i need a query :
> > >> > select TABLE1.x, count( TABLE2.subitem)

> >
> > >> > i guess the natural way should a TABLE1 left outer join
> > >> > TABLE2 with a count(*) and a group by TABLE1.*
> > >> > but i dont like the "group by table1.* "

> >
> > >> Why don't you like this?
> > >> Why would you group by table1.* when you are querying
> > >> table1.x?

> >
> > > oops, i just this :
> > > select TABLE1.* ,count(TABLE2.IDT2) from TABLE1 left outer
> > > join TABLE2 using (IDT1) group by TABLE1.IDT1

> >
> > > and it works
> > > i dont know why, but i was thinking that you need to use
> > > all select column in the group by

> >
> > The following might be of interest to you:
> >
> > http://dev.mysql.com/tech-resources/...group-by-myths...
> >
> > --
> > Pavel Lepin- Hide quoted text -
> >
> > - Show quoted text -

>
> But he was violating even the myth by wanting all the columns in the
> group by.
>


i wanted all the column in the select clause, not the group clause (but IMO,
i was forced to put them in the group one)



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:21 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0