4

pulling some info from a MySQL db via openquery:

select *
from foo
where bar not in (select bar from foobar)

Now, if I replace the subquery with hardwired numbers, it works fine, but i have near a 1000 numbers i need to exclude. I haven't been able to figure this out; both queries run fine by themselves. the error always tells me I have an syntax error in 'select bar from foobar)'

thanks

Edit:

here is the error:

[MySQL][ODBC 3.51 Driver][mysqld-4.0.20-log]You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select bar from foobar)' at line 3".
Limey
  • 2,642
  • 6
  • 37
  • 62

2 Answers2

3

The problem can be one, or more of the following situations:

  1. foo is not a table
  2. bar is not a column from foo table
  3. foobar is not a table
  4. bar is not a column from foobar table
  5. bar from foo table and bar from foobar have different (not compatible) datatypes

It's possibly a BUG from MYSQL4.0 (check HERE for that info).

Use NOT EXISTS instead of NOT IN like this:

select *
from foo
where bar NOT EXISTS (select bar from foobar WHERE foobar.bar = foo.bar)
aF.
  • 64,980
  • 43
  • 135
  • 198
  • hmmm....it could be number 5. I don't have the greatest access to the box, so I don't know how the columns were setup datatype wise. I will dig into this. – Limey May 23 '12 at 16:18
  • If it works when you put numbers, bar in foo table must be a number datatype and possibly it's not in bar from foobar table. – aF. May 23 '12 at 16:24
  • I checked both columns were numbers by attempting math on them, both allowed for propeer math (both interger and float) to be done on them – Limey May 23 '12 at 16:32
  • From more searching I have been doing, it looks like this is an issue with openquery on the SQL Server side, which I should of guess right off after some basic syntax was erroring. – Limey May 23 '12 at 16:39
  • @Limey check my edit and try what I've put. Prolly should work as a fix for now. – aF. May 23 '12 at 16:42
1

Are there any NULLs in your foobar subquery? This would mess up your approach. If so, more details at SQL "select where not in subquery" returns no results

Community
  • 1
  • 1
William Entriken
  • 37,208
  • 23
  • 149
  • 195