Solved SQL/Query <> not working (1 Viewer)

PatAccess

Registered User.
Local time
Today, 06:12
Joined
May 24, 2017
Messages
284
Hello guys,
I noticed that "<>" has not been working for several of my queries in the query design view and the SQL View. Is there a reason why?
I have 2 queries and I want records where the 2 states are not equal but it is returning all the states from one of the query
Here is the SQL View code
Code:
SELECT Qry_States.State
FROM QryBobRegistration, Qry_States
WHERE(((Qry_States.State)<>[QryBobRegistration].[StateRegistered]));

In the Design View, I have: <>[QryBobRegistration].[StateRegistered] on the Criteria Row.

What am I missing?
 

jdraw

Super Moderator
Staff member
Local time
Today, 06:12
Joined
Jan 23, 2006
Messages
15,378
Can you post the sql for the queries?

Can you provide some samples of this? Or a plain English overview of your requirement.
I want records where the 2 states are not equal
 

Cronk

Registered User.
Local time
Today, 20:12
Joined
Jul 4, 2013
Messages
2,772
What am I missing?
A closing parenthesis?

WHERE(((Qry_States.State)<>[QryBobRegistration].[StateRegistered])));
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:12
Joined
Oct 29, 2018
Messages
21,467
Did that query use to work before? As written, I don't know if it ever did. If you had two tables and both had CA and NV on them, for example, your query would return both states because CA is not equal to NV and NV is not equal to CA.
 

plog

Banishment Pending
Local time
Today, 05:12
Joined
May 11, 2011
Messages
11,645
You have 2 data sources in your FROM which results in a Cartesian product. That means every record in one data source will match with every record in the other data source. You need to JOIN them somehow. Most likely a LEFT JOIN.

Perhaps you can give us sample data from both the datasources and then what data you expect the query to return when you feed in that data.
 

Minty

AWF VIP
Local time
Today, 11:12
Joined
Jul 26, 2013
Messages
10,371
Left Join the two queries on State and select IS Null in the criteria on one side?
 

PatAccess

Registered User.
Local time
Today, 06:12
Joined
May 24, 2017
Messages
284
Can you post the sql for the queries?

Can you provide some samples of this? Or a plain English overview of your requirement.
I just thought about something. I am basically looking to return an unmatched query so I think plog is right because I used Join and it is working now. BUT I would still like to understand why "<>" seems not to work in this case?
If I say give me records from QryA where QryA.field <> QryB.field, why would it not return anything, or all values in QryA?
 

PatAccess

Registered User.
Local time
Today, 06:12
Joined
May 24, 2017
Messages
284
You have 2 data sources in your FROM which results in a Cartesian product. That means every record in one data source will match with every record in the other data source. You need to JOIN them somehow. Most likely a LEFT JOIN.

Perhaps you can give us sample data from both the datasources and then what data you expect the query to return when you feed in that data.
The LEFT JOIN worked. Thank you. I just saw that it was basically an unmatched query but I am still not understanding why "<>" never works
 

plog

Banishment Pending
Local time
Today, 05:12
Joined
May 11, 2011
Messages
11,645
Table1
[F1]
A
B
C

Table2
[F2]
A
B


SELECT [F1], [F2] FROM Table1, Table2

This is a simple Cartesian Product, it matches every value in Table1 with every value in Table2 because there is no relationship. These are the results of that query:

[F1], [F2]
A, A
A, B
B, A
B, B
C, A
C, B


Now we add in your criteria in the form of the WHERE:

SELECT [F1], [F2], FROM Table1, Table2 WHERE [F1]<>[F2]

[F1], [F2]
A, B
B, A
C, A
C, B

It eliminates the matched pairs of values. Every value in Table1 still remains as does every value of Table2 beause they have non-matching data in the other table. That's how your query was working.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:12
Joined
Oct 29, 2018
Messages
21,467
Hi. In your original query, you asked to return only the state from one table. In @plog's example, the states from both tables are returned. But in both occasions, you'll see that all the states from each table are returned because of what I said earlier. A<>B<>C<>B<>A.

So, it doesn't matter really whether you use a JOIN or not, the <> wasn't what you wanted to use.

In other words, neither if these will work.

select * from a, b where a<>b

or

select * from a join b on a<>b
 

PatAccess

Registered User.
Local time
Today, 06:12
Joined
May 24, 2017
Messages
284
Table1
[F1]
A
B
C

Table2
[F2]
A
B


SELECT [F1], [F2] FROM Table1, Table2

This is a simple Cartesian Product, it matches every value in Table1 with every value in Table2 because there is no relationship. These are the results of that query:

[F1], [F2]
A, A
A, B
B, A
B, B
C, A
C, B


Now we add in your criteria in the form of the WHERE:

SELECT [F1], [F2], FROM Table1, Table2 WHERE [F1]<>[F2]

[F1], [F2]
A, B
B, A
C, A
C, B

It eliminates the matched pairs of values. Every value in Table1 still remains as does every value of Table2 beause they have non-matching data in the other table. That's how your query was working.
Makes sense. Thank you for your help
 

Users who are viewing this thread

Top Bottom