473,661 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return rows which satisfy BOTH of my WHERE CLAUSES, not EITHER???

Imagine I had a table called MyTable with the following values:

Field1 Field2
____ _____
A X
AA R
B X
BB T

Now, say I want to return only rows which match *BOTH* of the
following criteria:

Criteria 1: Field1 Like A%
Criteria 2: Field2 = 'X'

The sql I've written is not giving me the specific results I want:

SELECT * FROM MyTable WHERE Field1 Like 'A%' And Field2 = 'X'
returns:

Field1 Field2
____ _____
A X
AA R
B X

....Now, I understand how this fufills the sql statement I'm writing,
but that statement must not be expressing what I really want. What I
really want is a result set consisting only of rows which match BOTH
of my criteria; not *either* of my criteria. If I wrote the sql
statement correctly, the result set would/should be this:

Field1 Field2
____ _____
A X

....You see the difference I'm sure. The above represents the only
record in MyTable in which Field1 starts with the letter "A"
***AND*** Field2 is an "X".

I know this is a very very basic question to ask, but could someone
help me with the sql statement I'm looking for? Thanks in advance.
Jul 20 '05 #1
2 1619
My apologies. My previous question on this subject was misleading I
think. I tried to simplify the example in my post and did it in such
a way that the problem I'm actually having wasn't expressed.

The actual situation is this: I'm not just performing a WHERE against
a single table. I'm doing it at the end of a 'join'-oriented
statement:

SELECT MyTable1.*, MyTable2.MyTabl e2Field, MyTable3.MyTabl e3Field
FROM (MyTable1
LEFT JOIN MyTable3 ON MyTable1.TableI DField = MyTable3.TableI DField)
LEFT JOIN MyTable2 ON MyTable1.id_MyT able2Field =
MyTable2.id_MyT able2Field
WHERE (MyTable2.MyTab le2Field='X')
AND (MyTable3.MyTab le3Field LIKE 'A%')

.... The (unwanted) result of this statement is a set of rows from
MyTable1 which have 'X' in the MyTable2.MyTabl e2Field column **OR**
begin with 'A' in the MyTable3.MyTabl e3Field field.

Again, what I'm after is a result set which will consist only of rows
which satisfy **ALL** of my where conditions. Specifically, every row
in the result set should begin with the letter 'A' in the
MyTable3.MyTabl e3Field column **AND** have 'X' as the value in the
MyTable2.MyTabl e2Field column.

I apologize for the over-simplified form of the earlier message. But,
I still need help! Thanks.

On Thu, 20 May 2004 16:32:32 GMT, Alan Mailer
<cl**********@e arthlink.net> wrote:
Imagine I had a table called MyTable with the following values:

Field1 Field2
____ _____
A X
AA R
B X
BB T

Now, say I want to return only rows which match *BOTH* of the
following criteria:

Criteria 1: Field1 Like A%
Criteria 2: Field2 = 'X'

The sql I've written is not giving me the specific results I want:

SELECT * FROM MyTable WHERE Field1 Like 'A%' And Field2 = 'X'
returns:

Field1 Field2
____ _____
A X
AA R
B X

...Now, I understand how this fufills the sql statement I'm writing,
but that statement must not be expressing what I really want. What I
really want is a result set consisting only of rows which match BOTH
of my criteria; not *either* of my criteria. If I wrote the sql
statement correctly, the result set would/should be this:

Field1 Field2
____ _____
A X

...You see the difference I'm sure. The above represents the only
record in MyTable in which Field1 starts with the letter "A"
***AND*** Field2 is an "X".

I know this is a very very basic question to ask, but could someone
help me with the sql statement I'm looking for? Thanks in advance.


Jul 20 '05 #2
Alan Mailer wrote:
Imagine I had a table called MyTable with the following values:

Field1 Field2
____ _____
A X
AA R
B X
BB T

Now, say I want to return only rows which match *BOTH* of the
following criteria:

Criteria 1: Field1 Like A%
Criteria 2: Field2 = 'X'

The sql I've written is not giving me the specific results I want:

SELECT * FROM MyTable WHERE Field1 Like 'A%' And Field2 = 'X'
returns:

Field1 Field2
____ _____
A X
AA R
B X

...Now, I understand how this fufills the sql statement I'm writing,
but that statement must not be expressing what I really want. What I
really want is a result set consisting only of rows which match BOTH
of my criteria; not *either* of my criteria. If I wrote the sql
statement correctly, the result set would/should be this:

Field1 Field2
____ _____
A X

...You see the difference I'm sure. The above represents the only
record in MyTable in which Field1 starts with the letter "A"
***AND*** Field2 is an "X".

I know this is a very very basic question to ask, but could someone
help me with the sql statement I'm looking for? Thanks in advance.

Try:

SELECT * FROM MyTable WHERE (Field1 Like 'A%' And Field2 = 'X')

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
12835
by: KENNY L. CHEN | last post by:
Dear experts, I have two tables in my Oracle 8i database: TEST (COL1,COl2,REC_NO) and TEST1 (COL1,COL2,REC_NO). Both tables are unique-indexed on (COL1,COL2,REC_NO). I think the following SQL commands will return the same result but one of my friends don't think so. He said "QUERY 1" will return 1 unsorted record (ROWNUM < 2 ) first then sort the result (ORDER BY COL1 ASC,
6
4351
by: Toucan | last post by:
i need to retrieve the most recent timestamped records with unique names (see working query below) what i'm having trouble with is returning the next-most-recent records (records w/ id 1 and 3 in this example) i also need to return the 3rd most recent, 4th, 5th and 6th most recent - i figure if i can get the 2nd working, 3rd, 4th, etc will be cake thanks,
1
1377
by: Eric Goforth | last post by:
Hello, When I remove MYTABLE5.HAPPYCAPACITY from the SELECT clauses of the following query, the number of rows returned drops from 28 to 24. If I put it back in it goes back up to 28. Any idea how this is possible? Thanks, Eric
7
1835
by: MLH | last post by:
?dcount("","qryOwnrsDueITSwMissingAddr") when run in the immediate window return a number greater than the number of rows that display when the saved query is run - opening in the database window? I consistently see 2 rows in qryOwnrsDueITSwMissingAddr opening with a dbl-clik. Yet ?dcount("","qryOwnrsDueITSwMissingAddr") returns 3 and
10
2029
by: el__marcin | last post by:
hi I am using MS office Access 2003. ANybody knows how to convert more than 300-400K rows from text into e.g. number ? I tried to do so a few times and access told me there aint enough memory or something. Anybody knows quick fix to that problem ? thanks
6
4018
by: rshivaraman | last post by:
CREATE TABLE ( (10) NULL ) CREATE TABLE ( (10) NULL )
7
4548
by: jb1 | last post by:
Hello All, I am trying to create a DTS package. I have two tables tbl_A and tbl_B with similar data/rows but no primary keys. tbl_A is master. I would like this package to query tbl_A and tbl_B and find 1)all rows in tbl_A that are different in tbl_B, 2)all rows in tbl_A that are not present in tbl_B and
27
2490
by: junky_fellow | last post by:
Guys, Can I return 0, from main() ? Is this equivalent to exit(EXIT_SUCCESS) ? thanks for any help...
101
4293
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : char *tmp = NULL;
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.