views:

68

answers:

3

I wrote the following query, I think it's correct but I have a "missing operator" error.

SELECT * FROM results,Types WHERE results.a=Types.b  
INTERSECT  SELECT * FROM results,Types WHERE results.c=Types.b

Could somebody help me please?

Thanks a lot.

+3  A: 

What database are you using? Are you sure that intersect is supported? I tried your query on Oracle (changing the table names to something corresponding to my DB) and it worked ok.

EDIT: Since you confirmed you are using MS-Access, then it's clear that INTERSECT is the problem since it's not supported with MS-Access: http://www.access-programmers.co.uk/forums/archive/index.php/t-86531.html

EDIT2: This is untested, but the basic idea is that you need to find all rows in your first query that exist in your second query. To do that, you will have to compare every column between the 2 queries for a match, as all the columns must match for it to be an "intersected" row.

There may be some syntax issues, but hopefully this gets you started.

SELECT r.col1
     , t.col1
     /* list all other columns here */
  FROM results r
     , types t
 WHERE r.a = t.b
 AND EXISTS (
     SELECT *
       FROM results r2
          , types   t2
      WHERE r2.c = t2.b
        AND NZ(r.col1,0) = NZ(r2.col1,0)
        AND NZ(t.col1,0) = NZ(t2.col1,0)
        /* list other columns here, they all need to match so intersection will work */
 )
dcp
I use MSAccess. I am not sure, I will check now.
Shadi
you're right. it does not support INTERSECTION. So, what should I do if I want to intersect them?
Shadi
@Shadi - Yes, it's unfortunate that intersection isn't available. It means you'll have to do things the hard way, see EDIT2 above. I haven't tested it, but I think the idea should hopefully work.
dcp
@dcp `INTERSECT` treats NULLs as equal for comparison purposes so to reproduce that there would need to be a load of IFNULLs or whatever the Access syntax is.
Martin Smith
@Martin Smith - Thank you for your comment. I updated my answer to reflect this. Seems NZ is the equivalent of IFNULL in ACCESS.
dcp
+2  A: 

It's possible the non-ANSI join is confusing matters

SELECT * FROM results R JOIN Types T ON R.a = T.b  
INTERSECT
SELECT * FROM results R JOIN Types T ON R.c = T.b 
gbn
Even if they aren't confusing matters, implicit joins are a very poor technique and they should be replaced. Of course the query should not be using select * either!
HLGEM
@HLGEM: one thing at a time :-)
gbn
It did not work with JOIN as well. SELECT queries work separately, but not together.
Shadi
+2  A: 

I would suggest testing each of the SELECT statements separately. Make sure they work by themselves. Then do the INTERSECT.

If you are working with SQL Server, for example, that is not the correct way to refer to two tables. You would need to add a JOIN clause (with an ON specifying the columns to JOIN).

Even if it isn't SQL Server, you need to make sure that the queries each work on their own.

Edit: Someone else has asked here about How can I implement SQL INTERSECT and MINUS operations in MS Access. I see a difference of opinion there, so be sure to test your results to make sure you're getting what you want.

DOK
the "SELECT"s work well separately, but they don't work together even with "JOIN".
Shadi