If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Rows of tbl1 that arent present in tbl2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-22-06, 14:41
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Rows of tbl1 that arent present in tbl2

Hi

I got 2 tables and I want rows from tbl1 that ids arent present in tbl2.
Code:
SELECT `id`, `info`
FROM `tbl1`
WHERE `id` <> (SELECT `id` FROM `tbl2`)
I get the error :
#1242 - Subquery returns more than 1 row
Any idea how to make the query work ?

Thanks
Reply With Quote
  #2 (permalink)  
Old 03-22-06, 15:12
gtkuntz gtkuntz is offline
Registered User
 
Join Date: Mar 2006
Posts: 2
use
where 'id' not in (select id........)
Reply With Quote
  #3 (permalink)  
Old 03-22-06, 15:50
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Ah...Thanks a lot !

Edit : BTW, Where do I search for this in MySQL 3.x,4.x docs ? Im using ver 5.x on my PC but host has 3.23 and want to know of NOT IN is acceptable in 3.x and 4.x.

Last edited by anjanesh; 03-22-06 at 21:45.
Reply With Quote
  #4 (permalink)  
Old 03-26-06, 10:02
benanat benanat is offline
Registered User
 
Join Date: Mar 2006
Posts: 7
use left join and is null to get what u want,
like this:

*************
SELECT
ID
FROM
TableA
LEFT JOIN
TableB
ON
(TableA.ID = TableB.ID)
WHERE
(TableB.ID IS NULL)
**********************
this is exectly what u need
Reply With Quote
  #5 (permalink)  
Old 03-26-06, 15:49
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,258
Quote:
Originally Posted by anjanesh
Ah...Thanks a lot !

Edit : BTW, Where do I search for this in MySQL 3.x,4.x docs ? Im using ver 5.x on my PC but host has 3.23 and want to know of NOT IN is acceptable in 3.x and 4.x.
it would seem sense to run the same version (certainly the same major version) as your ISP. so either develop on a version 3.23 MySQL, or find an ISP which supports version 5
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #6 (permalink)  
Old 06-24-06, 23:16
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Quote:
Originally Posted by gtkuntz
use
where 'id' not in (select id........)
Code:
SELECT `ID`, `info` FROM `tbl1` tbl1
WHERE tbl1.`ID` NOT IN (SELECT `ID` FROM `tbl1`)
is returning just 1 row when it should be returning more.
Any idea why ?

Using MySQL 5.0.22
__________________
MySQL 5.1
Reply With Quote
  #7 (permalink)  
Old 06-25-06, 04:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by anjanesh
Any idea why ?
yes, because you mentioned the wrong table in the subquery
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 06-25-06, 05:30
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Code:
SELECT `ID`,`info` FROM `tbl1`
WHERE `ID` NOT IN (SELECT `ID` FROM `tbl2`)
returns 0
__________________
MySQL 5.1
Reply With Quote
  #9 (permalink)  
Old 06-25-06, 08:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
okay, why don't you try benanat's solution
Code:
select tbl1.id
     , tbl1.info 
  from tbl1
left outer
  join tbl2
    on tbl2.id = tbl1.id 
 where tbl2.id is null
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 06-25-06, 08:13
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Sometimes the JOIN statements are taking more than 10 secs to execute.
benanat's solution did work though.

Btw, Im executing all these in phpMyAdmin.
__________________
MySQL 5.1
Reply With Quote
  #11 (permalink)  
Old 06-25-06, 08:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
make sure that each id column has an index (note: if it's a primary key, it already has an index)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On