how to select this relation?

This is a discussion on how to select this relation? within the MySQL Database forums, part of the Database Forums category; Hi there, assume we got two tables A and B. Both Tables contain names, while B contains a subset of ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-15-2007
T. S.
 
Posts: n/a
Default how to select this relation?

Hi there,
assume we got two tables A and B.
Both Tables contain names, while B contains a subset of A thus every
name that is in B can also be found in A.
What I want to do is select all names from A and (and this is my
problem) the count of occurrences of every name in Table B.

Example:

A.name:
adam
bob

B.name:
bob


I'm getting as far as this:
select A.name, count(B.name) as times_in_B from A join B on a.name =
b.name;

But:
The resulting relation contains only names that are in both tables, A
and B, meaning it skips adam:

A.name = bob
times_in_B = 1
..
What I want it to do is select all names from A and return a zero as
times_in_B if the name doesn't ocurr in B at all:

A.name = Adam, Bob
times_in_B = 0, 1

How can I do this?

Thanks in advance.

Reply With Quote
  #2 (permalink)  
Old 03-15-2007
Captain Paralytic
 
Posts: n/a
Default Re: how to select this relation?

On 15 Mar, 10:50, "T. S." <tstoff...@gmail.com> wrote:
> Hi there,
> assume we got two tables A and B.
> Both Tables contain names, while B contains a subset of A thus every
> name that is in B can also be found in A.
> What I want to do is select all names from A and (and this is my
> problem) the count of occurrences of every name in Table B.
>
> Example:
>
> A.name:
> adam
> bob
>
> B.name:
> bob
>
> I'm getting as far as this:
> select A.name, count(B.name) as times_in_B from A join B on a.name =
> b.name;
>
> But:
> The resulting relation contains only names that are in both tables, A
> and B, meaning it skips adam:
>
> A.name = bob
> times_in_B = 1
> .
> What I want it to do is select all names from A and return a zero as
> times_in_B if the name doesn't ocurr in B at all:
>
> A.name = Adam, Bob
> times_in_B = 0, 1
>
> How can I do this?
>
> Thanks in advance.


Look at LEFT JOIN and IFNULL()

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 07:50 AM.


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