Bluehost.com Web Hosting $6.95

HELP with SQL subselect

This is a discussion on HELP with SQL subselect within the MySQL Database forums, part of the Database Forums category; i am trying a query like select email from table_1 where email not in (select email from table_2); for some ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-30-2006
starman7@hotmail.com
 
Posts: n/a
Default HELP with SQL subselect

i am trying a query like

select email from table_1 where email not in (select email from
table_2);


for some reason, i am getting (at least one) email rows returned which
also appear in table_2 ...
i have tried using LOWER(email) for both selects, thought the email i
am getting is all lowercase in both tables.

my goal is to only return emails that appear in table_1 but not if they
also appear in table_2.

is there some way to handle this with LIKE or am i missing something in
that these are 'chars' (could the @ in emails be causing problems)?


might there be some other way to do this - using mysql 5.x


many thanks,
s7

Reply With Quote
  #2 (permalink)  
Old 11-30-2006
Michael Austin
 
Posts: n/a
Default Re: HELP with SQL subselect

starman7@hotmail.com wrote:

> i am trying a query like
>
> select email from table_1 where email not in (select email from
> table_2);
>
>
> for some reason, i am getting (at least one) email rows returned which
> also appear in table_2 ...
> i have tried using LOWER(email) for both selects, thought the email i
> am getting is all lowercase in both tables.
>
> my goal is to only return emails that appear in table_1 but not if they
> also appear in table_2.
>
> is there some way to handle this with LIKE or am i missing something in
> that these are 'chars' (could the @ in emails be causing problems)?
>
>
> might there be some other way to do this - using mysql 5.x
>
>
> many thanks,
> s7
>

This can be faster than subselects...

first, need more information - like what is the relationship between table1 and
table2?

select a.email from table1 a left join table2 b on a.email=b.email where b.email
is null;


--
Michael Austin.
Database Consultant
Reply With Quote
  #3 (permalink)  
Old 11-30-2006
starman7@hotmail.com
 
Posts: n/a
Default Re: HELP with SQL subselect


Michael Austin wrote:
> starman7@hotmail.com wrote:
>
> > i am trying a query like
> >
> > select email from table_1 where email not in (select email from
> > table_2);
> >
> >
> > for some reason, i am getting (at least one) email rows returned which
> > also appear in table_2 ...
> > i have tried using LOWER(email) for both selects, thought the email i
> > am getting is all lowercase in both tables.
> >
> > my goal is to only return emails that appear in table_1 but not if they
> > also appear in table_2.
> >
> > is there some way to handle this with LIKE or am i missing something in
> > that these are 'chars' (could the @ in emails be causing problems)?
> >
> >
> > might there be some other way to do this - using mysql 5.x
> >
> >
> > many thanks,
> > s7
> >

> This can be faster than subselects...
>
> first, need more information - like what is the relationship between table1 and
> table2?
>
> select a.email from table1 a left join table2 b on a.email=b.email where b.email
> is null;



thanks for the reply michael -

there is no relationship between the two tables - only the email which
may be the same in either - with the added problem that the case may be
different - any way to deal with this (w/ the lower keyword)?

also to add another wrinkle, i would actually like to exclude from a
third table - can two left joins be used? it's less complex with two
NOT IN subselects (would that be an OR or an AND between the two
subselects?) - anyway - i used your example and still an email address
that was in table2 and also in table1 is coming back in my resultset
(it's the same case in both tables)

the columns are both varchar255 - and there are no spaces in either
result.

select a.email from t1 a left join t2 b on a.email=b.email where
b.email
is null;


thanks again,
s7


>
>
> --
> Michael Austin.
> Database Consultant


Reply With Quote
  #4 (permalink)  
Old 12-01-2006
Michael Austin
 
Posts: n/a
Default Re: HELP with SQL subselect

starman7@hotmail.com wrote:

> Michael Austin wrote:
>
>>starman7@hotmail.com wrote:
>>
>>
>>>i am trying a query like
>>>
>>>select email from table_1 where email not in (select email from
>>>table_2);
>>>
>>>
>>>for some reason, i am getting (at least one) email rows returned which
>>>also appear in table_2 ...
>>>i have tried using LOWER(email) for both selects, thought the email i
>>>am getting is all lowercase in both tables.
>>>
>>>my goal is to only return emails that appear in table_1 but not if they
>>>also appear in table_2.
>>>
>>>is there some way to handle this with LIKE or am i missing something in
>>>that these are 'chars' (could the @ in emails be causing problems)?
>>>
>>>
>>>might there be some other way to do this - using mysql 5.x
>>>
>>>
>>>many thanks,
>>>s7
>>>

>>
>>This can be faster than subselects...
>>
>>first, need more information - like what is the relationship between table1 and
>>table2?
>>
>>select a.email from table1 a left join table2 b on a.email=b.email where b.email
>>is null;

>
>
>
> thanks for the reply michael -
>
> there is no relationship between the two tables - only the email which
> may be the same in either - with the added problem that the case may be
> different - any way to deal with this (w/ the lower keyword)?
>
> also to add another wrinkle, i would actually like to exclude from a
> third table - can two left joins be used? it's less complex with two
> NOT IN subselects (would that be an OR or an AND between the two
> subselects?) - anyway - i used your example and still an email address
> that was in table2 and also in table1 is coming back in my resultset
> (it's the same case in both tables)
>
> the columns are both varchar255 - and there are no spaces in either
> result.
>
> select a.email from t1 a left join t2 b on a.email=b.email where
> b.email
> is null;
>
>
> thanks again,
> s7
>
>
>
>>
>>--
>>Michael Austin.
>>Database Consultant

>
>


an easier way to deal with email addresses is to use lower() when you insert
them, that way there is less data processing when needing to do the selects

(to modify what you currently have simply:
update tablea set email=lower(email);
this will change all email address to lower case. If you are using a web-page
to insert/query/login use a function to lowercase the user input to match the
database.
)

adding where clause an

select a.email,b.email,c.email from t1 a
left join t2 b
on lower(a.email)=lower(b.email)
left join t3 c
on lower(a.email)=lower(c.email);
+---------+---------+---------+
| email | email | email |
+---------+---------+---------+
| a@b.com | a@b.com | a@b.com | <<exists in all 3
| b@c.com | b@c.com | NULL | <<exists in t1,t2
| c@d.com | NULL | c@d.com | <<exists in t1,t3
+---------+---------+---------+
3 rows in set (0.04 sec)

The BASE query;
mysql> select a.email,b.email,c.email from t1 a left join t2 b on lower(a.email
)=lower(b.email) left join t3 c on lower(a.email)=lower(c.email);
+---------+---------+---------+
| email | email | email |
+---------+---------+---------+
| a@b.com | a@b.com | a@b.com |
| b@c.com | b@c.com | NULL |
| c@d.com | NULL | c@d.com |
+---------+---------+---------+
3 rows in set (0.04 sec)

ROWS in A but may not exist in B or in C
mysql> select a.email,b.email,c.email from t1 a left join t2 b on lower(a.email
)=lower(b.email) left join t3 c on lower(a.email)=lower(c.email) where b.email i
s null or c.email is null;
+---------+---------+---------+
| email | email | email |
+---------+---------+---------+
| b@c.com | b@c.com | NULL |
| c@d.com | NULL | c@d.com |
+---------+---------+---------+
2 rows in set (0.02 sec)

ROWS in a but may exist in b or c
mysql> select a.email,b.email,c.email from t1 a left join t2 b on lower(a.email
)=lower(b.email) left join t3 c on lower(a.email)=lower(c.email) where b.email i
s not null or c.email is not null;
+---------+---------+---------+
| email | email | email |
+---------+---------+---------+
| a@b.com | a@b.com | a@b.com |
| b@c.com | b@c.com | NULL |
| c@d.com | NULL | c@d.com |
+---------+---------+---------+
3 rows in set (0.04 sec)


Records that exist in all 3 tables;
mysql> select a.email,b.email,c.email from t1 a left join t2 b on lower(a.email
)=lower(b.email) left join t3 c on lower(a.email)=lower(c.email) where b.email i
s not null and c.email is not null;
+---------+---------+---------+
| email | email | email |
+---------+---------+---------+
| a@b.com | a@b.com | a@b.com |
+---------+---------+---------+
1 row in set (0.08 sec)


or this will find rows where the email ONLY exists in t1.
First - let's add a row to t1 only:

mysql> insert into t1 values ('d@f.com');
Query OK, 1 row affected (0.22 sec)

select f.email from t1 f where f.email not in (
select a.email
from t1 a left outer join t2 b
on a.email=b.email
left outer join t3 c
on a.email=c.email
where b.email is not null or c.email is not null
);

+---------+
| email |
+---------+
| d@f.com |
+---------+


--
Michael Austin.
DBA Consultant
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
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 03:02 PM.


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