Unix Technical Forum

Find all rows with no matching rows in second table

This is a discussion on Find all rows with no matching rows in second table within the MySQL forums, part of the Database Server Software category; --> Hello group, I have two tables: Table A a_id name 1 a 2 b 3 c Table B b_id ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 09:12 AM
André Hänsel
 
Posts: n/a
Default Find all rows with no matching rows in second table

Hello group,

I have two tables:

Table A
a_id name
1 a
2 b
3 c

Table B
b_id a_id flag name
1 2 y x
2 2 n y
3 3 n z

How can I find the rows from table A where there is no matching row (joined
using a_id as key) in table B where flag is "y"?

So in this example I want the entries 1/a and 3/c from table A. 2/b should
not be selected because there is a row in table B with a_id = 2 and
flag="y".

Understandable?

It seems quite impossible to me, but I cannot figure out a reason why it is
impossible, either.

Regards,
André


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 09:12 AM
Captain Paralytic
 
Posts: n/a
Default Re: Find all rows with no matching rows in second table


André Hänsel wrote:
> Hello group,
>
> I have two tables:
>
> Table A
> a_id name
> 1 a
> 2 b
> 3 c
>
> Table B
> b_id a_id flag name
> 1 2 y x
> 2 2 n y
> 3 3 n z
>
> How can I find the rows from table A where there is no matching row (joined
> using a_id as key) in table B where flag is "y"?
>
> So in this example I want the entries 1/a and 3/c from table A. 2/b should
> not be selected because there is a row in table B with a_id = 2 and
> flag="y".


Try:
SELECT A.a_id, A.name
FROM Table_A A
LEFT JOIN Table_B B ON A.a_id = B.a_id AND B.flag = "y"
WHERE B.b_id IS NULL

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 09:12 AM
Peter H. Coffin
 
Posts: n/a
Default Re: Find all rows with no matching rows in second table

On Mon, 18 Sep 2006 23:16:42 +0200, André Hänsel wrote:
> So in this example I want the entries 1/a and 3/c from table A. 2/b should
> not be selected because there is a row in table B with a_id = 2 and
> flag="y".
>
> Understandable?
>
> It seems quite impossible to me, but I cannot figure out a reason why it is
> impossible, either.


It is time for homework questions already?

--
_ o
|/)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 09:13 AM
=?iso-8859-1?Q?Andr=E9_H=E4nsel?=
 
Posts: n/a
Default Re: Find all rows with no matching rows in second table

Peter H. Coffin wrote:
> On Mon, 18 Sep 2006 23:16:42 +0200, André Hänsel wrote:
>> So in this example I want the entries 1/a and 3/c from table A. 2/b
>> should not be selected because there is a row in table B with a_id =
>> 2 and flag="y".
>>
>> Understandable?
>>
>> It seems quite impossible to me, but I cannot figure out a reason
>> why it is impossible, either.

>
> It is time for homework questions already?


Hm? What do you want to say? That I haven't made my homework?

What did I overlook?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:38 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com